简体   繁体   中英

How can I merge two structs in Golang?

I have two json-marshallable anonymous structs.

a := struct {
    Name string `json:"name"`
}{"my name"}

b := struct {
    Description string `json:"description"`
}{"my description"}

Is there any way to merge them into json to get something like that:

{
    "name":"my name",
    "description":"my description"
}

You can embed both structs in another .

type name struct {
    Name string `json:"name"`
}

type description struct {
    Description string `json:"description"`
}

type combined struct {
    name
    description
}

The JSON package will treat embedded structs kind of like unions, but this can get clunky pretty quickly.

It's a bit convoluted but I suppose you could do something like this:

    a := struct {
        Name string `json:"name"`
    }{"my name"}

    b := struct {
        Description string `json:"description"`
    }{"my description"}

    var m map[string]string

    ja, _ := json.Marshal(a)
    json.Unmarshal(ja, &m)
    jb, _ := json.Marshal(b)
    json.Unmarshal(jb, &m)

    jm, _ := json.Marshal(m)
    fmt.Println(string(jm))

You can merge two struct like this :

package main

import (
    "fmt"
    "encoding/json"
)


type b struct {
    Name string  `json:"name"`
    Description string
    Url string
}

type a struct {
    *b
    MimeType string  `json:"mimeType"`
}

func main() {
    bc := b{"test", "testdecription", "testurl"}
    ac := a{nil, "jpg"}

    ac.b = &bc

    js, _ := json.Marshal(ac)

    fmt.Println(string(js) )
}

Go is all about composition over inheritance. Sadly, you're using anonymous structs, but given that you're clearly trying to json marshal them, you're better of defining them as types:

type name struct {
    Name string `json:"name"`
}
type desc struct {
    Description string `json:"description"`
}

You can initialize them in the same way as you're currently doing, but instead of struct{<fields>}{init} , you'll just write

a := name{"foo"}
b := desc{"Description"}

You can then combine them in any way you like by writing:

c := struct {
    name
    description
}{a, b}

One quirk (that might trip you up at first) you have to get used to when composing types like this is how you initialize the members. Say you decide to create a type that combines the other two structs:

type foo struct {
    name
    description
}

You cannot initialize it like this:

o := foo{"Name value", "description value"}

Go will complain about you using type string as type name . You'll have to write something like this:

o := foo{
    name{"Name value"},
    description{Description: "Description val"},//optional with field names
}

An in-line composite built using existing objects (cf c := struct{}{a, b} ) does this already.
Depending on what you're trying to do, it sometimes is easier to write something like this:

func (s *MyCompositeType) CopyName(n name) {
    s.Name = n.Name
    //copy other fields
}

It makes life easier when you're nesting composite types several levels deep.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM