Golang enum najlepsza praktyka
package main
import "fmt"
type CustomType int
func (enum CustomType) Enum(index CustomType, data []string) string {
return data[index]
}
func main() {
// example one created enum type
const (
Admin = iota
User
Staff
)
var RolesEnum CustomType = Admin | User | Staff
data := []string{"Admin", "User", "Staff"}
fmt.Printf("enum version one %#v \n", RolesEnum.Enum(RolesEnum&Admin, data))
// example two created enum type
var EnumStatusList = struct {
Completed string
Rejected string
Error string
}{
Completed: "Completed",
Rejected: "Rejected",
Error: "Error",
}
fmt.Printf("enum version two %#v \n", EnumStatusList.Completed)
// example three created enum type
const (
Kucing = "Kucing"
Gajah = "Gajah"
Biawak = "Biawak"
)
fmt.Printf("enum version three %#v \n", Kucing)
}
Restu Wahyu Saputra