1.golang structのフィールドにomitemptyを指定した時の振る舞い
・nilを入れた時にフィールドが省略されている
2.code
package main import ( "encoding/json" "fmt" ) type Employee_s struct { Num *interface{} `json:"num,omitempty"` } func main() { var NULL interface{}; employee := [7] Employee_s{} var isNull_1,isNull_2,isNull_3,isNull_4,isNull_5 interface{}; isNull_1 = nil isNull_2 = false isNull_3 = true isNull_4 = 0 isNull_5 = "" employee[0].Num = &isNull_1; employee[1].Num = &isNull_2; employee[2].Num = &isNull_3; employee[3].Num = &isNull_4; employee[4].Num = &isNull_5; employee[5].Num = nil employee[6].Num = &NULL json_string, _ := json.Marshal(employee) fmt.Println(string(json_string)) }
3.結果
[{"num":null},{"num":false},{"num":true},{"num":0},{"num":""},{},{"num":null}]