Hoang Tan
Hoang Tan

Reputation: 11

How to add Tags to Struct by function in golang

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.

type Items []Item

type Item struct {
    Id        string `json:"id"        required:"true"`
    CompCd    string `json:"compCd"    required:"true"`
    OrgCd     string `json:"orgCd"`
    QstnId    string `json:"qstnId"    required:"true"`
    QstnIdSeq string `json:"qstnIdSeq" required:"true"`
    CustId    string `json:"custId"`
    AnsDts    string `json:"ansDts"    required:"true"`
    AnsRout   string `json:"ansRout"   required:"true"`
    QCd01     string `json:"qCd01"`
    QKey01    string `json:"qKey01"`
    QStc01    string `json:"qStc01"`
    QCat01    string `json:"qCat01"`
    Pont01    string `json:"pont01"`
    PCat01    string `json:"pCat01"`
    Comt01    string `json:"comt01"`
    QCd02     string `json:"qCd02"`
    QKey02    string `json:"qKey02"`
    QStc02    string `json:"qStc02"`
    QCat02    string `json:"qCat02"`
    Pont02    string `json:"pont02"`
    PCat02    string `json:"pCat02"`
    Comt02    string `json:"comt02"`
    .
    .
    .
    QCd50  string `json:"qCd50"`
    QKey50 string `json:"qKey50"`
    QStc50 string `json:"qStc50"`
    QCat50 string `json:"qCat50"`
    Pont50 string `json:"pont50"`
    PCat50 string `json:"pCat50"`
    Comt50 string `json:"comt50"`
}  

Upvotes: 0

Views: 2676

Answers (1)

Juan Carlos Garcia
Juan Carlos Garcia

Reputation: 533

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.

Upvotes: 3

Related Questions