猿问

除了使用接口和使用扩展运算符之外如何创建动态结构

我正在尝试使用 Go 和 Fiber 框架创建新的 API。我正在使用 MongoDB。请参考我下面的结构


type Product struct {

    Name           string              `json:"name"`

    Code           string              `json:"code"`

    Description    string              `json:"description"`

    Brand          string              `json:"brand"`

    Variants       []map[string]string `json:"variants"`

    Categories     []int               `json:"categories"`

}

Variants 字段将有另一组对象数组。但是这些领域将是动态的。所以我无法定义结构。现在我用 []map[string][string] 解决了这个问题。


我在这里的任务是用以下格式形成 CSV 文件。


[{

    Name: "Rock",

    Code: "RRR"

    Description: "This is rock"

    Brand: "R"

    ...variants

},

{

    Name: "Rock",

    Code: "RRR"

    Description: "This is rock"

    Brand: "R"

    ...variants

},

{

    Name: "Rambo",

    Code: "RAM"

    Description: "This is Rambo"

    Brand: "RA"

    ...variants

},

{

    Name: "Rambo",

    Code: "RAM"

    Description: "This is Rambo"

    Brand: "RA"

    ...variants

}]

每行只有变体会发生变化。其他人将保持不变,我形成了除变体之外的其他领域。我无法创建结构,因为数据是动态的。


我有很多困惑


如何为动态字段创建结构

如何在根级别传播变体

我来自javascript。所以,我们使用了传播运算符。在这里,我对如何在 Golang 中做到这一点感到困惑。


动漫人物
浏览 86回答 1
1回答

互换的青春

如果您愿意,可以使用反射动态构造新结构下面是一个例子:package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "reflect")type DynamicMap map[string]interface{}type Product struct {&nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `json:"name"`&nbsp; &nbsp; Code&nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `json:"code"`&nbsp; &nbsp; Description string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `json:"description"`&nbsp; &nbsp; Brand&nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `json:"brand"`&nbsp; &nbsp; Variants&nbsp; &nbsp; map[string]string `json:"variants"`&nbsp; &nbsp; Categories&nbsp; []int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`json:"categories"`}func (j Product) MarshalJSON() ([]byte, error) {&nbsp; &nbsp; m := DynamicMap{}&nbsp; &nbsp; t := reflect.TypeOf(j)&nbsp; &nbsp; v := reflect.ValueOf(j)&nbsp; &nbsp; for i := 0; i < t.NumField(); i++ {&nbsp; &nbsp; &nbsp; &nbsp; if t.Field(i).Tag.Get("json") == "variants" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dyn := v.Field(i).Interface().(map[string]string)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for key, val := range dyn {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m[key] = val&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else if t.Field(i).Type.Kind() == reflect.String {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m[t.Field(i).Tag.Get("json")] = v.Field(i).String()&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m[t.Field(i).Tag.Get("json")] = v.Field(i)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return json.Marshal(m)}func (j Product) UnmarshalJSON(data []byte) error {&nbsp; &nbsp; fmt.Println("Unmarshal...")&nbsp; &nbsp; return json.Unmarshal(data, &j)}func main() {&nbsp; &nbsp; p := Product{Name: "aa", Variants: map[string]string{"a": "a", "b": "b"}}&nbsp; &nbsp; marshal, err := json.Marshal(p)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%v\n", err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Printf("%s\n", marshal)}{"a":"a","b":"b","brand":"","categories":{},"code":"","description":"","name":"aa"}&nbsp;
随时随地看视频慕课网APP

相关分类

Go
我要回答