{"id":1216,"date":"2022-01-24T13:58:10","date_gmt":"2022-01-24T04:58:10","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=1216"},"modified":"2022-01-26T10:21:21","modified_gmt":"2022-01-26T01:21:21","slug":"go-database-sql-null%e3%81%ae%e6%89%b1%e3%81%84%e3%81%a7%e3%80%81sql-nullxxx%e3%82%92%e4%bd%bf%e3%82%8f%e3%81%aa%e3%81%84","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=1216","title":{"rendered":"<small>Golang database\/sql  \u691c\u7d22\u6642\u306eNULL\u306e\u6271\u3044\u3067\u3001interface {} \u3092\u4f7f\u3046<\/small>"},"content":{"rendered":"<p>\uff11\uff0eNULL\u306e\u6271\u3044\u65b9\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u7b49\u3067\u306e\u4f8b\u3067\u306e\u7591\u554f<\/p>\n<p>Golang\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u7b49\u3067\u3001database\/sql \u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u578b\u3067\u3042\u308b\u3001sql.NullString\u3001<br \/>\nsql.NullInt64\u7b49\u3092\u5229\u7528\u3057\u305f\u4f8b\u304c\u691c\u7d22\u3067\u5f15\u304d\u5f53\u305f\u308b\u3002<br \/>\n\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001\u53d6\u308a\u51fa\u3057\u3001Print\u3059\u308b\u3068\u3053\u308d\u307e\u3067\u306f\u3001\u8a18\u8ff0\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n<p>\u3060\u304c\u3001\u5b9f\u969b\u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306f\u3001\u305d\u308c\u3067\u7d42\u308f\u308a\u3067\u306f\u306a\u3044\u3002<\/p>\n<p>\uff12\uff0e\u4f55\u304c\u3044\u3044\u305f\u3044\u304b<\/p>\n<p>\u53d6\u308a\u51fa\u3057\u305f\u5024\u3092Struct\u3067\u5b9a\u7fa9\u3057\u305ffield\u7b49\u306b\u4ee3\u5165\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u306b\u306a\u308c\u3070\u3001<br \/>\n\u305d\u3053\u3067\u578b\u5909\u63db\u3092\u884c\u3046\u3053\u3068\u306b\u306a\u308b\u3002<br \/>\n\u305d\u3082\u305d\u3082\u3001null\u306e\u578b\u3068int\u307e\u305f\u306fstring\u306e\u578b\u3092\u540c\u3058\u5909\u6570\u306b\u5165\u308c\u3088\u3046\u3068\u3059\u308b\u3068<br \/>\ninterface\u578b\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3002<\/p>\n<p>\u8981\u306f\u3001sql.NullString\u3001sql.NullInt64\u7b49\u306e\u578b\u3092\u52a0\u3048\u308b\u3068\u3001\u6df7\u4e71\u3059\u308b\u3060\u3051\u3002<\/p>\n<p>\uff13\uff0e\u3067\u306f\u3069\u3046\u3059\u308b\u304b\uff1f<\/p>\n<p>sql.NullString\u3001sql.NullInt64\u7b49\u3092\u4f7f\u7528\u3057\u306a\u304f\u3066\u3082\u3001Null\u5024\u306finterface\u578b\u3067\u5224\u5b9a\u3067\u304d\u308b\u3002<br \/>\n\u53d6\u308a\u51fa\u3057\u305f\u3068\u304d\u306e\u5224\u5b9a\u3068Type\u5909\u63db\u304c\u5fc5\u8981\u3060\u304c\u3001\u3053\u306e\u5909\u63dbcode\u306f\u76f4\u89b3\u7684\u306b\u7406\u89e3\u3067\u304d\u308b\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u6700\u521d\u304b\u3089interface {} \u3067\u53d6\u308a\u51fa\u3057\u305f\u65b9\u304c\u7c21\u5358\u3060\u3068\u3044\u3046\u3053\u3068\u3002<\/p>\n<p>\uff14\uff0einterface\u578b\u3067\u53d6\u308a\u51fa\u3057\u305f\u5024<br \/>\n\u30fbnull \u306f nil\u306b\u306a\u308b<\/p>\n<p>\u5f93\u3063\u3066\u3001\u5024\u304c nil\u304b\u3069\u3046\u304b\u5224\u5b9a\u3057\u3066\u3001\u76ee\u7684\u306eType\u306b\u5909\u63db\u3057\u3066\u3084\u308c\u3070\u826f\u3044\u3002<\/p>\n<p>\uff15\uff0estep<\/p>\n<p>(1) db.Query\u306eSELECT\u6587\u3067\u3001interface\u578b\u3067\u53d6\u308a\u51fa\u3059<br \/>\n(2) \u6b21\u306b\u3001\u53d6\u308a\u51fa\u3057\u305f\u5024\u304cnil\u304b\u3069\u3046\u304b\u3067\u5224\u5b9a\u3059\u308b<br \/>\n(3) \u5024\u304cnil\u3067\u306a\u3051\u308c\u3070\u3001\u76ee\u7684\u306e\u578b\u306b\u5909\u63db\u3059\u308b<\/p>\n<p>\uff16\uff0ecode\u306e\u66f8\u304d\u3084\u3059\u3055<\/p>\n<p>sql.NullString\u3001sql.NullInt64\u7b49\u3092\u5229\u7528\u3059\u308b\u3088\u308a\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u3066\u3044\u3066\u3059\u3063\u304d\u308a\u3059\u308b\u3002<\/p>\n<p>\uff16\uff0ecode<br \/>\n\u30fb\u4f8b\u306etax_id\u306f\u3001DB\u306etable\u306ecolumn\u5b9a\u7fa9\u3067null or string\u306e\u524d\u63d0<\/p>\n<pre>\r\ntype tax_id_s struct {\r\n    Tax_id  interface{} `json:\"tax_id\"`\r\n}\r\n\u30fb\u30fb\u30fb\r\n   var _r tax_id_s;\r\n\r\n   SQL := `\r\n      SELECT\r\n         tax_id,\r\n      FROM\r\n         taxt_list\r\n   `;\r\n\r\n   rows, _ := auth_db.Query(s.String())\r\n\r\n   for rows.Next() {\r\n      _ = rows.Scan( &_r.Tax_id )\r\n\r\n      if _r.Tax_id != nil {\r\n         _r.Tax_id = fmt.Sprintf(\"%s\", _r.Tax_id);\r\n      }\r\n   }\r\n\u30fb\u30fb\u30fb\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uff11\uff0eNULL\u306e\u6271\u3044\u65b9\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u7b49\u3067\u306e\u4f8b\u3067\u306e\u7591\u554f Golang\u306e\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u7b49\u3067\u3001database\/sql \u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u578b\u3067\u3042\u308b\u3001sql.NullString\u3001 sql.NullInt64\u7b49\u3092\u5229\u7528\u3057\u305f\u4f8b\u304c\u691c\u7d22\u3067\u5f15\u304d&#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\/1216"}],"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=1216"}],"version-history":[{"count":26,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":1245,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions\/1245"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}