简体   繁体   English

转到如何检查产品类型

[英]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 ,该函数将:

  • check if the name is a valid one 检查名称是否有效
  • return one of the official 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.

相关问题 编译时间类型检查 - Compile time type check 给定参数的ParameterInfo时,如何检查服务方法的参数类型是否为输出类型? - How do I check if a service method's parameter type is an output type given the parameter's ParameterInfo? 如何在 Magento 2 中按产品 ID 获取每个产品的评论百分比? - How to get the percentage of reviews of each product by product id in Magento 2? 更改名称和批量更新Joomla / Virtuemart产品类型参数 - Change name and Bulk Update Joomla / Virtuemart Product Type Parameters 如何获取通用产品描述作为 DialogFlow 中的参数? - How to get a generic product description as a parameter in DialogFlow? 检查 char * 类型的字符串是否包含另一个字符串 - Check if a string of type char * contains another string 如何在Go中将Infix(比较)运算符用作参数 - How to Use Infix (Comparison) operators as parameters in Go 如何在 Go 中强制将参数作为指针传递? - How to force passing parameter as a pointer in Go? 如何在 Go 中有一个带有可为空字符串参数的 function? - How to have a function with a nullable string parameter in Go? 参数,参数及其去向—如何使用术语 - Arguments, Parameters, and Where They Go — How to Use the Terms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM