如何组合2个结构内容,其中一个具有相同的键

I have two structs, one with more keys than the other, with fewer keys and more identical. I want to give less content to multiple keys struct at once, how to do it?

type moreStruct struct {
    A string `json:"a"`
    B string `json:"b"`
    C string `json:"c"`
    D string `json:"d"`
    E string `json:"e"`
}

type leseStruct struct {
    A string `json:"a"`
    B string `json:"b"`
    D string `json:"d"`
}

more := moreStruct{
        A: "aaa",
        B: "bbb",
        C: "ccc",
        D: "ddd",
        E: "eee",
}
less := leseStruct{
        A: "aaaaaaa",
        B: "bbbbbbb",
        D: "ddddddd",
}

//hava any better mothod than below in one line
more.A = less.A
more.B = less.B
more.D = less.D

Right, so if you can change the types/structs themselves, you can use embedding so you can reassign the entire subset of fields. That does mean you'll have to change the way you write the literals a bit, and because you're using json, the embedded type will need to be exported.

// Less, the smallest subset of fields that are shared
type Less struct {
    A string `json:"a"`
    B string `json:"b"`
    D string `json:"d"`
}

// More the type that has all the fields in Less + some of its own
type More struct {
    Less // embed Less in this type
    C string `json:"c"`
    E string `json:"e"`
}

Now that we've got these types, this is how you initialise the fields in a literal:

more := More{
    Less: Less{
        A: "aaa",
        B: "bbb",
        D: "ddd",
    },
    C: "ccc",
    E: "eee",
}
// or the dirty way (no field names) - don't do this... it's nasty
yuck := More{
    Less{
        "a",
        "b",
        "d",
    },
    "c",
    "e",
}

Now, say we've got a variable less like so:

less := Less{
    A: "aaaaa",
    B: "bbbbb",
    D: "ddddd",
}

And now we want to copy over these values to the variable more we've created above:

more.Less = less

Job done... Because the Less type is embedded, json marshalling and unmarshalling will still work in the same way

Demo