mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
854 B
854 B
Enums
In Go, enums are often defined as a type and a set of constants. For example:
type MyEnum int
const (
MyEnumOne MyEnum = iota
MyEnumTwo
MyEnumThree
)
Due to incompatibility between Go and JavaScript, custom types cannot be used in this way. The best strategy is to use a type alias for float64:
type MyEnum = float64
const (
MyEnumOne MyEnum = iota
MyEnumTwo
MyEnumThree
)
In Javascript, you can then use the following:
const MyEnum = {
MyEnumOne: 0,
MyEnumTwo: 1,
MyEnumThree: 2,
};
- Why use
float64? Can't we useint?- Because JavaScript doesn't have a concept of
int. Everything is anumber, which translates tofloat64in Go. There are also restrictions on casting types in Go's reflection package, which means usingintdoesn't work.
- Because JavaScript doesn't have a concept of