英文:
How to make a struct field be a value from list?
问题
我有一个访问权限列表:
const (
Everyone = 0
Owner = 1
Administrator = 2
)
还有一个表示路由的结构体:
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
AccessLevel uint64
}
如何限制Route
结构体中的AccessLevel
字段值只能是上述常量之一?
英文:
I have a list of access rights:
const (
Everyone = 0
Owner = 1
Administrator = 2
)
And a struct representing Routes:
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
AccessLevel uint64
}
How can I restrict AccessLevel
field value of the Route
struct be only one of those const from above?
答案1
得分: 1
这种限制的唯一方法是不导出该字段,并在任何setter方法中进行检查。
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
accessLevel uint64
}
// AccessLevel的getter方法
func (r Route) AccessLevel() uint64 {
return r.accessLevel
}
// SetAccessLevel的setter方法
func (r Route) SetAccessLevel(value uint64) error {
if value < 0 || value > 2 {
return errors.New("AccessLevel必须介于0和2之间(包括0和2)")
}
r.accessLevel = value
return nil
}
英文:
The only way to impose this type of restriction is by not exporting the field, and doing your checks in any setter method(s).
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
accessLevel uint64
}
// AccessLevel getter method
func (r Route) AccessLevel() uint64 {
return r.accessLevel
}
// SetAccessLevel setter method
func (r Route) SetAccessLevel(value uint64) error {
if value < 0 || value > 2 {
return errors.New("AccessLevel must be between 0 and 2, inclusive")
}
r.accessLevel = value
return nil
}
答案2
得分: 0
你不能这样做。Go语言不支持枚举类型。不过,你可以接近实现它。只需为你的访问级别引入一个单独的类型:
type AccessLevel uint64
并将常量定义为该类型:
const (
Everyone AccessLevel = 0
Owner = 1
Administrator = 2
)
然后将字段定义为该类型:
type Route struct {
// ...
AccessLevel AccessLevel
}
现在你可以将这些常量赋值给AccessLevel
类型的变量,但不能赋值给一个“普通”的uint64
类型的变量。
需要注意的是,你仍然可以通过类型转换将uint64
转换为AccessLevel
类型,例如AccessLevel(1234)
。
英文:
You can't. Go doesn't support enums. However, you can get close to it. Just introduce a separate type for your access level:
type AccessLevel uint64
and make the consts be of the type:
const (
Everyone AccessLevel = 0
Owner = 1
Administrator = 2
)
Then define the Field to be of that type:
type Route struct {
// ...
AccessLevel AccessLevel
}
Now you can assign those consts, but not a "normal" uint64
.
The caveat is that you can still type convert a uint64
to a AccessLevel
via AccessLevel(1234)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论