darccio / mergo

Mergo: merging Go structs and maps since 2013

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to overwrite specified field with empty value in struct

elinxz opened this issue · comments

I just want to overwrite the value of the specified field of type string, is there any way to make it.

type MergeData struct {
	A string `json:"a"` // overwrite with empty value
	B string `json:"b"` // overwrite with non-empty value
}

func TestA(t *testing.T) {
	src := &MergeData{
		A: "",
		B: "",
	}
	dst := &MergeData{
		A: "a",
		B: "b",
	}
	err := mergo.Merge(dst, src, mergo.WithOverride)
	if err != nil {
		panic(err)
	}
	t.Log(dst)
}

How to get {"", "b"}?

Mergo doesn't support, and won't in the mid term, a selective merging operation like this.