github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/g/tags.go (about) 1 package g 2 3 import "strings" 4 5 // TagOptions contains a slice of tag options 6 type TagOptions []string 7 8 // Has returns true if the given option is available in TagOptions 9 func (t TagOptions) Has(opt string) bool { 10 for _, tagOpt := range t { 11 if tagOpt == opt { 12 return true 13 } 14 } 15 16 return false 17 } 18 19 // ParseTag splits a struct field's tag into its name and a list of options 20 // which comes after a name. A tag is in the form of: "name,option1,option2". 21 func ParseTag(tag string) (string, TagOptions) { 22 // tag is one of followings: 23 // "" 24 // "name" 25 // "name,opt" 26 // "name,opt,opt2" 27 // ",opt" 28 29 res := strings.Split(tag, ",") 30 return res[0], res[1:] 31 }