[英]Go How to check type of product
I have model Product
with field Type
. 我有模型Product
与字段Type
。
Something like this: 像这样:
type ProductType string
var (
PtRouteTransportation ProductType = "ProductRT"
PtOnDemandTransportation ProductType = "ProductDT"
PtExcursion ProductType = "ProductEX"
PtTicket ProductType = "ProductTK"
PtQuote ProductType = "ProductQT"
PtGood ProductType = "ProductGD"
)
type Product struct {
...
Type ProductType
...
}
In Create
function I have type
form param: 在Create
函数中,我具有type
表单参数:
type := req.Form.Get("type")
Question: how to check is type
valid? 问题:如何检查type
有效?
Simplest way is: 最简单的方法是:
if type != PtRouteTransportation && type != PtOnDemandTransportation && ...
but what I supposed to do if Product
will have 100 types? 但是,如果Product
有100种类型,我该怎么办?
How to do this in go
way? 如何做到这一点的go
呢?
Instead of using a type alias to a basic type, why not using a type alias to a private type (meaning a struct you cannot initialize outside your package) 为何不对私有类型使用类型别名,而不是对基本类型使用类型别名(这意味着您不能在包外部初始化结构)
See this example . 请参阅此示例 。
type ProductType productType
type productType struct {
name string
}
var (
PtRouteTransportation ProductType = ProductType(productType{"ProductRT"})
PtOnDemandTransportation ProductType = ProductType(productType{"ProductDT"})
PtExcursion ProductType = ProductType(productType{"ProductEX"})
PtTicket ProductType = ProductType(productType{"ProductTK"})
PtQuote ProductType = ProductType(productType{"ProductQT"})
PtGood ProductType = ProductType(productType{"ProductGD"})
)
func printProductType(pt ProductType) {
fmt.Println(pt.name)
}
func main() {
fmt.Println("Hello, playground")
// printProductType("test") // cannot use "test" (type string) as type ProductType in argument to printProductType
printProductType(PtRouteTransportation)
}
It means you cannot use any other values than the public ones defined in the var
section. 这意味着您不能使用var
部分中定义的公共值以外的任何其他值。
If you manage to pass a value to the printProductType( pt ProductType
), your value is always a valid one. 如果您设法将一个值传递给printProductType( pt ProductType
),则您的值始终是有效值。
For the dynamic checking part, that OneOfOne addresses in his answer , I would add a function GetProductType(name string) ProductType
which: 对于动态检查部分, OneOfOne 在其答案中解决了这个问题 ,我将添加一个函数GetProductType(name string) ProductType
,该函数将:
ProductType
instances defined in the var section above. 返回上面的var部分中定义的官方ProductType
实例之一。 That way, the rest of your code always work with an official ProductType value (and not with a 'string' which happens to be matching the right value) 这样,您的其余代码将始终与正式的ProductType值一起使用(而不是与恰好匹配正确值的“字符串”一起使用)
Really the simplest is to use a map, not as fast as constants but if if you have to test against a large set, it's the most convenient way. 真正最简单的方法是使用映射,而不是像常量那样快,但是如果必须对大型集合进行测试,则这是最方便的方法。
Also since it's pre-allocated, it's thread-safe, so you won't have to worry about locks, unless you add to it at runtime. 另外,由于它是预先分配的,因此它是线程安全的,因此您不必担心锁,除非您在运行时添加锁。
var (
ptTypes = map[string]struct{}{
"ProductRT": {},
"ProductDT": {},
"ProductEX": {},
"ProductTK": {},
"ProductQT": {},
"ProductGD": {},
}
)
func validType(t string) (ok bool) {
_, ok = ptTypes[t]
return
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.