{"id":1039,"date":"2022-01-11T11:24:27","date_gmt":"2022-01-11T02:24:27","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=1039"},"modified":"2022-01-12T08:38:19","modified_gmt":"2022-01-11T23:38:19","slug":"golang-db%e3%81%a7bigint%e3%81%a7%e5%ae%9a%e7%be%a9%e3%81%97%e3%81%9funix-time%e3%82%92struct%e3%81%b8%e4%bb%a3%e5%85%a5%e3%81%a7%e3%80%81database-sql-pkg%e3%81%aenull%e5%9e%8b%e3%81%ae%e7%a2%ba","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=1039","title":{"rendered":"<small>golang DB\u306ecolumn\u306bBIGINT\u3067\u5b9a\u7fa9\u3057\u305fUnix time\u3092struct\u306b\u4ee3\u5165<\/small>"},"content":{"rendered":"<p>\uff11\uff0e\u6761\u4ef6<br \/>\nDB\u306ecolumn\u306bBIGINT\u3067\u5b9a\u7fa9\u3057\u305fUnix time\u3092field\u3092inteface\u578b\u3067\u5ba3\u8a00\u3057\u305fstruct\u306b\u4ee3\u5165\u3059\u308b<br \/>\n\u3053\u306e\u6642\u3001database\/sql pkg\u3092Valid\u3067Null\u306e\u78ba\u8a8d<\/p>\n<p>\uff12\uff0e\u7d50\u8ad6<br \/>\nsql.NullString \u3092\u4f7f\u3046\u5834\u5408\u3068sql.NullInt64 \u3092\u4f7f\u3046\u5834\u5408\u3067\u7d50\u679c\u306f\u540c\u3058<\/p>\n<p>\uff13\uff0e\u51e6\u7406\u306eStep<br \/>\n(1) DB\u5074\uff1aNULL\u3092\u542b\u3080BIGINT\u3067\u5b9a\u7fa9\u3057\u305f\u3082\u306e\u306bUnix time\u3092\u4fdd\u5b58<br \/>\n(2) DB\u304b\u3089Query\u306eSELECT\u6587\u3067Unix time\u3092\u53d6\u308a\u51fa\u3059<br \/>\n(3) \u53d6\u308a\u51fa\u3059\u5909\u6570\u306e\u578b\u3092\u5909\u3048\u3066\u307f\u308b<br \/>\n\u30fbsql.NullString \u3092\u4f7f\u3046\u5834\u5408<br \/>\n\u30fbsql.NullInt64 \u3092\u4f7f\u3046\u5834\u5408<br \/>\n(4) Unix time\u3092field\u3092inteface\u578b\u3067\u5ba3\u8a00\u3057\u305fstruct\u306b\u4ee3\u5165\u3059\u308b<\/p>\n<p>\uff14\uff0ecode :Query\u3067sql.NullString\u3092\u4f7f\u3044\u53d6\u308a\u51fa\u3057\u305f\u5834\u5408\u3001Sprintf\u306f&#8221;%s&#8221;\u3067\u5909\u63db<\/p>\n<pre>\r\ntype session_s struct {\r\n   Session_id    string      `json:\"session_id\"`\r\n   Login_time    interface{} `json:\"login_time\"`\r\n   Logout_time   interface{} `json:\"logout_time\"`\r\n}\r\nvar _r session_s_s;\r\n\u30fb\u30fb\u30fb\r\nvar session_id string;\r\nvar login , logout sql.NullString;\r\nvar NULL interface {};\r\n\r\nfor rows.Next() {\r\n   _ = rows.Scan( &session_id, &login, &logout);\r\n\r\n   if login.Valid {\r\n      r.Login_time = fmt.Sprintf(\"%s\",login.String);\r\n   } else {\r\n      _r.Login_time = NULL;\r\n   }\r\n   if logout.Valid {\r\n      _r.Logout_time = fmt.Sprintf(\"%s\",logout.String);\r\n   } else {\r\n      _r.Logout_time = NULL;\r\n   }\r\n   fmt.Println(\"{\",_r.Login_time,\":\", _r.Logout_time,\"}\");\r\n}\r\n<\/pre>\n<p>\uff15\uff0e\u5b9f\u884c\u7d50\u679c<\/p>\n<pre>\r\n{ 1641787646985 : 1641792373964 }\r\n{ 1641792428859 : \uff1cnil> }\r\n{ 1641852057966 : \uff1cnil> }\r\n{ 1641852062604 : 1641856423049 }\r\n{ 1641856427860 : \uff1cnil> }\r\n<\/pre>\n<p>\uff16\uff0ecode Query\u3067sql.NullInt64\u3092\u4f7f\u3044\u53d6\u308a\u51fa\u3057\u305f\u5834\u5408\u3001Sprintf\u306f&#8221;%d&#8221;\u3067\u5909\u63db<\/p>\n<pre>\r\ntype session_s struct {\r\n   Session_id    string      `json:\"session_id\"`\r\n   Login_time    interface{} `json:\"login_time\"`\r\n   Logout_time   interface{} `json:\"logout_time\"`\r\n}\r\nvar _r session_s_s;\r\n\u30fb\u30fb\u30fb\r\nvar session_id string;\r\nvar login , logout sql.NullInt64;\r\nvar NULL interface {};\r\n\r\nfor rows.Next() {\r\n   _ = rows.Scan( &session_id, &login, &logout);\r\n\r\n   if login.Valid {\r\n      r.Login_time = fmt.Sprintf(\"%d\",login.String);\r\n   } else {\r\n      _r.Login_time = NULL;\r\n   }\r\n   if logout.Valid {\r\n      _r.Logout_time = fmt.Sprintf(\"%d\",logout.String);\r\n   } else {\r\n      _r.Logout_time = NULL;\r\n   }\r\n   fmt.Println(\"{\",_r.Login_time,\":\", _r.Logout_time,\"}\");\r\n}\r\n<\/pre>\n<p>\uff17\uff0e\u5b9f\u884c\u7d50\u679c<\/p>\n<pre>\r\n{ 1641787646985 : 1641792373964 }\r\n{ 1641792428859 : \uff1cnil> }\r\n{ 1641852057966 : \uff1cnil> }\r\n{ 1641852062604 : 1641856423049 }\r\n{ 1641856427860 : \uff1cnil> }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uff11\uff0e\u6761\u4ef6 DB\u306ecolumn\u306bBIGINT\u3067\u5b9a\u7fa9\u3057\u305fUnix time\u3092field\u3092inteface\u578b\u3067\u5ba3\u8a00\u3057\u305fstruct\u306b\u4ee3\u5165\u3059\u308b \u3053\u306e\u6642\u3001database\/sql pkg\u3092Valid\u3067Null\u306e\u78ba\u8a8d \uff12\uff0e\u7d50\u8ad6 sq&#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\/1039"}],"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=1039"}],"version-history":[{"count":14,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/1039\/revisions"}],"predecessor-version":[{"id":1081,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/1039\/revisions\/1081"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}