dongqiyou0303 2018-11-12 00:24
浏览 109
已采纳

从参数动态填充结构

I've the following struct which works as expected Im getting data and I was able

type Service struct {
    Resources    []struct {
        Model struct {
            Name                string `json:"name"`
            Credentials         struct {
                path string `json:"path"`
                Vts struct {
                    user string `json:"user"`
                    id    string `json:"id"`
                    address      string `json:"address"`
                } `json:"vts"`
            } `json:"credentials"`
        } `json:"model"`
    } `json:"resources"`
}

service:= Service{}
err := json.Unmarshal([]byte(data), &service

The data is like following,

service1

{
    "resources": [
        "model": {
          "name": "cred",
          "credentials": {
            "path": "path in fs",
            "vts": {
              "user": "stephane", 
              "id": "123",
              "address": "some address"
            }
          },
        }
      },

Now some services providing additional data under vts for example now we have 3 fields (user/id/address) but some services (service1) can provides additional data like email, secondName ,etc . but the big problem here is that I need to get it from parameters since (service 2) education, salary etc

Service2

{
    "resources": [
        "model": {
          "name": "cred",
          "credentials": {
            "path": "path in fs",
            "vts": {
              "user": "stephane",
              "id": "123",
              "address": "some address",
              "email" : "some email",
              "secondName" : "secondName"
            }
          },
        }
      },

service N

{
    "resources": [
        "model": {
          "name": "cred",
          "credentials": {
            "path": "path in fs",
            "vts": {
              "user": "stephane",
              "id": "123",
              "address": "some address",
              "salary" : "1000000"
            }
          },
        }
      },

Of course If I know in advance the fields I can put them all in the struct and use omitempty but I dont know, I just get it as parameter to the function (the new properties names) , some service can provide 10 more fields in this struct (which I should get the properties name of them as args[]to the functions) but I don't know them in advance, this should be dynamic somehow ....is there a nice way to handle it in Golang ?

展开全部

  • 写回答

1条回答 默认 最新

  • drtohng5613 2018-11-12 00:45
    关注

    If you don't know the fields in advance, then don't use a struct but something that is also "dynamic": a map.

    type Service struct {
        Resources []struct {
            Model struct {
                Name        string `json:"name"`
                Credentials struct {
                    Path string                 `json:"path"`
                    Vts  map[string]interface{} `json:"vts"`
                } `json:"credentials"`
            } `json:"model"`
        } `json:"resources"`
    }
    

    map[sting]interface{} can hold values of any type. If you know all fields will hold a string value, you may also use a map[string]string so it will be easier to work with it.

    Example with input JSON:

    {
        "resources": [
          {
            "model": {
              "name": "cred",
              "credentials": {
                "path": "path in fs",
                "vts": {
                  "user": "stephane", 
                  "id": "123",
                  "address": "some address",
                  "dyn1": "d1value",
                  "dyn2": "d2value"
                }
              }
            }
          }
        ]
    }
    

    Testing it:

    service := Service{}
    err := json.Unmarshal([]byte(data), &service)
    fmt.Printf("%q %v", service, err)
    

    Output (try it on the Go Playground):

    {[{{"cred" {"path in fs" map["dyn2":"d2value" "user":"stephane" "id":"123" 
        "address":"some address" "dyn1":"d1value"]}}}]} <nil>
    

    Now if you want to collect values from the Vts map for a set of keys, this is how you can do it:

    args := []string{"dyn1", "dyn2"}
    values := make([]interface{}, len(args))
    for i, arg := range args {
        values[i] = service.Resources[0].Model.Credentials.Vts[arg]
    }
    fmt.Println(values)
    

    Output of the above will be (try it on the Go Playground):

    [d1value d2value]
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部