在beego中是否可以使用多个注解?

huangapple go评论98阅读模式
英文:

Is it possible to use multiple annotations in beego?

问题

我有一个类似的模型:

type Service struct {
    Id       uint64 
    Name     string 
    Secret   string 
    Disabled bool
}

我想使用formvalidorm这样的注解。但是我找不到应该如何声明这些注解。是应该只有一个注解还是多个注解?如果是多个注解,应该使用什么分隔符?

英文:

I have model like:

type Service struct {
    Id       uint64 
    Name     string 
    Secret   string 
    Disabled bool
}

And want to use annotations like form, valid and orm. And I can't find how I should declare these annotations. Should it be one or many? If many, what separator should I use?

答案1

得分: 2

引用自reflect.StructTag

> 按照惯例,标签字符串是一个可选的以空格分隔的键值对的串联。

因此,您可以通过空格分隔来指定多个键值对,例如:

type Service struct {
    Id uint64 `form:"id" valid:"Range(1, 999)" orm:"auto"`
}

在这个答案中可以了解更多关于标签的内容:https://stackoverflow.com/questions/10858787/what-are-the-uses-for-tags-in-go/30889373#30889373

英文:

Quoting from reflect.StructTag:

> By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs.

So you may specify multiple key-value pairs separated by space, like:

type Service struct {
    Id uint64 `form:"id" valid:"Range(1, 999)" orm:"auto"`
}

See more about tags in this answer: https://stackoverflow.com/questions/10858787/what-are-the-uses-for-tags-in-go/30889373#30889373

huangapple
  • 本文由 发表于 2017年4月19日 22:21:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/43498452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定