mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 08:12:49 +08: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 tofloat64
in Go. There are also restrictions on casting types in Go's reflection package, which means usingint
doesn't work.
- Because JavaScript doesn't have a concept of