猿问

从参数动态填充结构

我有以下结构,它按预期工作我正在获取数据并且我能够


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

数据如下,


服务1


{

    "resources": [

        "model": {

          "name": "cred",

          "credentials": {

            "path": "path in fs",

            "vts": {

              "user": "stephane", 

              "id": "123",

              "address": "some address"

            }

          },

        }

      },

现在一些服务在 vts 下提供额外的数据,例如现在我们有 3 个字段(用户/ID/地址),但一些服务(服务 1)可以提供额外的数据,如电子邮件、secondName 等。但这里的大问题是我需要从参数中获取它,因为(服务 2)教育、薪水等


服务2


{

    "resources": [

        "model": {

          "name": "cred",

          "credentials": {

            "path": "path in fs",

            "vts": {

              "user": "stephane",

              "id": "123",

              "address": "some address",

              "email" : "some email",

              "secondName" : "secondName"

            }

          },

        }

      },

服务 N


{

    "resources": [

        "model": {

          "name": "cred",

          "credentials": {

            "path": "path in fs",

            "vts": {

              "user": "stephane",

              "id": "123",

              "address": "some address",

              "salary" : "1000000"

            }

          },

        }

      },

当然,如果我事先知道我可以将它们全部放入结构中并使用的字段,omitempty 但我不知道,我只是将它作为函数的参数(新属性名称)获取,一些服务可以在此结构中提供 10 个以上的字段(我应该得到它们关于args[]函数的属性名称)但我事先不知道它们,这应该是动态的......在 Golang 中有没有很好的方法来处理它?


忽然笑
浏览 110回答 1
1回答

幕布斯6054654

如果您事先不知道这些字段,则不要使用结构,而是使用同样“动态”的东西:地图。type Service struct {&nbsp; &nbsp; Resources []struct {&nbsp; &nbsp; &nbsp; &nbsp; Model struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp; string `json:"name"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Credentials struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Path string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`json:"path"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vts&nbsp; map[string]interface{} `json:"vts"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } `json:"credentials"`&nbsp; &nbsp; &nbsp; &nbsp; } `json:"model"`&nbsp; &nbsp; } `json:"resources"`}map[sting]interface{}可以保存任何类型的值。如果您知道所有字段都将保存一个string值,您也可以使用 amap[string]string以便更容易地使用它。输入 JSON 的示例:{&nbsp; &nbsp; "resources": [&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "model": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "cred",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "credentials": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "path": "path in fs",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "vts": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "user": "stephane",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "id": "123",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "address": "some address",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "dyn1": "d1value",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "dyn2": "d2value"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]}测试它:service := Service{}err := json.Unmarshal([]byte(data), &service)fmt.Printf("%q %v", service, err)输出(在Go Playground上尝试):{[{{"cred" {"path in fs" map["dyn2":"d2value" "user":"stephane" "id":"123"&nbsp;&nbsp; &nbsp; "address":"some address" "dyn1":"d1value"]}}}]} <nil>现在,如果你想从Vts映射中收集一组键的值,你可以这样做:args := []string{"dyn1", "dyn2"}values := make([]interface{}, len(args))for i, arg := range args {&nbsp; &nbsp; values[i] = service.Resources[0].Model.Credentials.Vts[arg]}fmt.Println(values)上面的输出将是(在Go Playground上试试):[d1value d2value]
随时随地看视频慕课网APP

相关分类

Go
我要回答