{"id":1134,"date":"2022-01-17T07:24:32","date_gmt":"2022-01-16T22:24:32","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=1134"},"modified":"2022-02-27T07:58:35","modified_gmt":"2022-02-26T22:58:35","slug":"golang-json-marshal%e3%81%a7-nill-null-%e3%82%92%e6%89%b1%e3%81%86","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=1134","title":{"rendered":"<small>Golang json.Marshal\u3067struct\u306bomitempty\u3092\u6307\u5b9a\u3057\u3066 nill -> null \u3092\u884c\u3046<\/small>"},"content":{"rendered":"<p>\uff11\uff0ejson.Marshal\u3067struct\u306bomitempty\u3092\u6307\u5b9a\u3057\u3066 nill -> null \u3092\u884c\u3046<br \/>\n(1) truct\u306bomitempty\u3092\u6307\u5b9a<br \/>\n(2) nill,\u3000&#8221;&#8221;\u3000,\u3000false\u3000\u3092\u5224\u5b9a<br \/>\n(3) nill  \u306e\u5834\u5408\u3001nill -> null \u3092\u884c\u3046<\/p>\n<p>\uff12\uff0e\u7d50\u8ad6<br \/>\n\u30fbomitempty\u3092\u6307\u5b9a\u3057\u3066\u3082\u3001\u5b9f\u884c\u7d50\u679c\u3067\u306f\u30d5\u30a3\u30fc\u30eb\u30c9\u306f\u7701\u7565\u3055\u308c\u3066\u3044\u306a\u3044<\/p>\n<p>\uff12\uff0ecode<\/p>\n<pre>\r\npackage main\r\n\r\nimport (\r\n    \"encoding\/json\"\r\n    \"fmt\"\r\n)\r\n\r\ntype Employee_s struct {\r\n    Name        interface{}  `json:\"name,omitempty\"`\r\n    Id          *interface{} `json:\"id,omitempty\"`\r\n}\r\n\r\nfunc main() {\r\n\r\n  var NULL interface{};\r\n    employee := [6] Employee_s{}\r\n\r\n    employee[0].Name = \"Name_1\"\r\n    employee[1].Name = nil\r\n    employee[2].Name = NULL\r\n    employee[3].Name = \"\"\r\n    employee[4].Name = ``\r\n    employee[5].Name = false\r\n\r\n    isNull_1 := interface{}(nil)\r\n    isNull_2 := interface{}(2)\r\n    isNull_3 := interface{}(\"\")\r\n\r\n    var isNull_4 interface{}\r\n    var isNull_5 interface{}\r\n    var isNull_6 interface{}\r\n\r\n    isNull_4 = nil\r\n    isNull_5 = false\r\n    isNull_6 = true\r\n\r\n    employee[0].Id = &isNull_1\r\n    employee[1].Id = &isNull_2\r\n    employee[2].Id = &isNull_3\r\n    employee[3].Id = &isNull_4\r\n    employee[4].Id = &isNull_5\r\n    employee[5].Id = &isNull_6\r\n\r\n    json_string, _ := json.Marshal(employee)\r\n\r\n    fmt.Println(string(json_string))\r\n}\r\n<\/pre>\n<p>\u30fb\u5b9f\u884c\u7d50\u679c<\/p>\n<pre>\r\n# go run test.go\r\n[{\"Name\":\"Name_1\",\"id\":null},{\"Name\":null,\"id\":2},{\"Name\":null,\"id\":\"\"},{\"Name\":\"\",\"id\":null},{\"Name\":\"\",\"id\":false},{\"Name\":false,\"id\":true}]\r\n#\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uff11\uff0ejson.Marshal\u3067struct\u306bomitempty\u3092\u6307\u5b9a\u3057\u3066 nill -> null \u3092\u884c\u3046 (1) truct\u306bomitempty\u3092\u6307\u5b9a (2) nill,\u3000&#8221;&#8221;\u3000,\u3000false\u3000&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/1134"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1134"}],"version-history":[{"count":20,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/1134\/revisions"}],"predecessor-version":[{"id":1136,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/1134\/revisions\/1136"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}