GoLang JSON封送处理忽略非简单类型的空-可能避免指针?

GoLang JSON封送处理忽略非简单类型的空-可能避免指针?,json,pointers,go,Json,Pointers,Go,下面的代码是解释。处理非简单类型的唯一方法是将该类型设为指针 有没有不使用指针的替代解决方案 代码不工作: type Foo struct { Bar Bar `json:"bar,omitempty"` } type Bar struct { Baz string `json:"baz"` } func main() { foo := Foo{} jsonBytes, _ := json.Marshal(foo) fmt.Printf("%s\

下面的代码是解释。处理非简单类型的唯一方法是将该类型设为指针

有没有不使用指针的替代解决方案

代码不工作:

type Foo struct {
    Bar Bar `json:"bar,omitempty"`
}

type Bar struct {
    Baz string `json:"baz"`
}

func main() {

    foo := Foo{}

    jsonBytes, _ := json.Marshal(foo)

    fmt.Printf("%s\n", jsonBytes)
}
输出:
{“bar”:{“baz”:“}}

代码正常工作,但不是我想要的:

type Foo struct {
    Bar *Bar `json:"bar,omitempty"`
}

type Bar struct {
    Baz string `json:"baz"`
}

func main() {

    foo := Foo{}

    jsonBytes, _ := json.Marshal(foo)

    fmt.Printf("%s\n", jsonBytes)
}
输出:
{}

有没有不使用指针的替代解决方案

没有