1 StructTag
1 | package main |
blue gopher
1 | package main |
tag: json:"foo,omitempty,string" tag: xml:"foo" json:"foo_bar" xml:"foo" hcl:"foo,squash"
2 AST(抽象语法树)
遍历语法树,展示结构体字段列表:
1 | package main |
Field: Foo Tag: `json:"foo"`
3 gomodifytags
1 | package main |
3.1 Struct based modification
gomodifytags -file demo.go -struct Server -add-tags json -w |
1 | package main |
支持三种 transform , 如下所示:
snakecase
: “BaseDomain” -> “base_domain”camelcase
: “BaseDomain” -> “baseDomain”lispcase
: “BaseDomain” -> “base-domain”
gomodifytags -file demo.go -struct Server -add-tags xml -transform camelcase |
1 | package main |
gomodifytags -file demo.go -struct Server -add-tags xml -transform camelcase |
1 | package main |
gomodifytags -file demo.go -struct Server -add-tags validate:gt=1,scope:read-only |
1 | package main |
gomodifytags -file demo.go -struct Server -add-tags json --add-options json=omitempty -w |
1 | package main |
gomodifytags -file demo.go -struct Server -remove-tags json |
1 | package main |
gomodifytags -file demo.go -struct Server -clear-tags |
1 | package main |
gomodifytags -file demo.go -struct Server -remove-options json=omitempty |
1 | package main |
gomodifytags -file demo.go -struct Server -clear-options |
1 | package main |
3.2 Line based modification
gomodifytags -file demo.go -line 8,11 -clear-tags json |
1 | package main |
gomodifytags -file demo.go -line 6,7 -remove-tags json |
1 | package main |
gomodifytags -file demo.go -line 2,7 -add-tags bson |
1 | package main |
gomodifytags -file demo.go -offset 100 -add-tags bson |
1 | package main |
3.3 Shortcoming
1 | package main |
gomodifytags -file user.go -struct User -add-tags json |
期望得到的结果是: first_name
,但是却得到了 first___name
, 同时 addr
字段不导出也生成了 tag
, 如下所示:
1 | package main |
Render by hexo-renderer-org with Emacs 25.3.2 (Org mode 8.2.10)