type User struct {
    Name       string `json:"name"`
    Email      string `json:"email,omitempty"`
    Age        int    `json: "age"`
    Addess     string `json:"-"`
    Amount     int    `json:"amount"`
    smallCount int    `json:"smallCount"`
}

func TestIgnore(tt *testing.T) {
    u1 := User{
        Name:       "asong",
        Age:        18,
        Addess:     "beijing",
        smallCount: 20,
    }
    b, err := json.Marshal(u1)
    if err != nil {
        fmt.Printf("json.Marshal failed, err:%v\n", err)
        return
    }
    fmt.Printf("str:%s\n", b)
    //str:{"name":"asong","Age":18,"amount":0}
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code