如何在Go语言中通过函数向结构体添加标签

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

How to add Tags to Struct by function in golang

问题

我从REST.body接收数据,然后使用json.Unmarshal将数据编码为以下结构体,然后创建data到file.txt。然后当file.txt创建后,我可以进行搜索。

我的问题是:结构体中的键太多,从1到50,如下所示。我不知道如何解决这个问题。有人建议使用map[string]string,但我不想将结构体更改为map。

  1. type Items []Item
  2. type Item struct {
  3. Id string `json:"id" required:"true"`
  4. CompCd string `json:"compCd" required:"true"`
  5. OrgCd string `json:"orgCd"`
  6. QstnId string `json:"qstnId" required:"true"`
  7. QstnIdSeq string `json:"qstnIdSeq" required:"true"`
  8. CustId string `json:"custId"`
  9. AnsDts string `json:"ansDts" required:"true"`
  10. AnsRout string `json:"ansRout" required:"true"`
  11. QCd01 string `json:"qCd01"`
  12. QKey01 string `json:"qKey01"`
  13. QStc01 string `json:"qStc01"`
  14. QCat01 string `json:"qCat01"`
  15. Pont01 string `json:"pont01"`
  16. PCat01 string `json:"pCat01"`
  17. Comt01 string `json:"comt01"`
  18. QCd02 string `json:"qCd02"`
  19. QKey02 string `json:"qKey02"`
  20. QStc02 string `json:"qStc02"`
  21. QCat02 string `json:"qCat02"`
  22. Pont02 string `json:"pont02"`
  23. PCat02 string `json:"pCat02"`
  24. Comt02 string `json:"comt02"`
  25. .
  26. .
  27. .
  28. QCd50 string `json:"qCd50"`
  29. QKey50 string `json:"qKey50"`
  30. QStc50 string `json:"qStc50"`
  31. QCat50 string `json:"qCat50"`
  32. Pont50 string `json:"pont50"`
  33. PCat50 string `json:"pCat50"`
  34. Comt50 string `json:"comt50"`
  35. }
英文:

I receive data from REST.body -> encoding data with json.Unmashal to Struct like below then create data to file.txt. Then i can search when file.txt created.
My problem: keys in struct too many from 1 - 50 like below. I don't know how to solve it. Some recommend is use map[string]string but i dont wanna change Struct to map.

  1. type Items []Item
  2. type Item struct {
  3. Id string `json:"id" required:"true"`
  4. CompCd string `json:"compCd" required:"true"`
  5. OrgCd string `json:"orgCd"`
  6. QstnId string `json:"qstnId" required:"true"`
  7. QstnIdSeq string `json:"qstnIdSeq" required:"true"`
  8. CustId string `json:"custId"`
  9. AnsDts string `json:"ansDts" required:"true"`
  10. AnsRout string `json:"ansRout" required:"true"`
  11. QCd01 string `json:"qCd01"`
  12. QKey01 string `json:"qKey01"`
  13. QStc01 string `json:"qStc01"`
  14. QCat01 string `json:"qCat01"`
  15. Pont01 string `json:"pont01"`
  16. PCat01 string `json:"pCat01"`
  17. Comt01 string `json:"comt01"`
  18. QCd02 string `json:"qCd02"`
  19. QKey02 string `json:"qKey02"`
  20. QStc02 string `json:"qStc02"`
  21. QCat02 string `json:"qCat02"`
  22. Pont02 string `json:"pont02"`
  23. PCat02 string `json:"pCat02"`
  24. Comt02 string `json:"comt02"`
  25. .
  26. .
  27. .
  28. QCd50 string `json:"qCd50"`
  29. QKey50 string `json:"qKey50"`
  30. QStc50 string `json:"qStc50"`
  31. QCat50 string `json:"qCat50"`
  32. Pont50 string `json:"pont50"`
  33. PCat50 string `json:"pCat50"`
  34. Comt50 string `json:"comt50"`
  35. }

答案1

得分: 3

正如Alex提到的,无法在运行时添加标签。然而,Fatih(vim-go的创建者,用于Go的Vim扩展)有一个命令行工具可以修改结构体的标签。这是该仓库,也许可以帮到你。

英文:

As Alex mentions, it is not possible to add tags at runtime. However, Fatih (creator of the vim extension for go, vim-go) has a cli-tool to modify the tags of a struct. Here is the repo, maybe it could help you.

huangapple
  • 本文由 发表于 2017年2月20日 10:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/42335433.html
匿名

发表评论

匿名网友

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

确定