{"id":206259,"date":"2025-06-12T09:39:30","date_gmt":"2025-06-12T01:39:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206259\/"},"modified":"2025-06-12T09:39:30","modified_gmt":"2025-06-12T01:39:30","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-db-queryrowsql-scan-%e7%bb%93%e6%9e%9c%e6%89%ab%e6%8f%8f%e8%87%b3-map%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206259\/","title":{"rendered":"\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f"},"content":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241107\/1730975268672c9624487a3.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5229\u7528 db.queryrow(sql).scan \u5c06\u7ed3\u679c\u96c6\u626b\u63cf\u81f3 map<\/strong>,\u5728 go \u4e2d\uff0cdb.queryrow(sql).scan \u53ea\u80fd\u5c06\u7ed3\u679c\u96c6\u626b\u63cf\u81f3\u53d8\u91cf\u6307\u9488\uff0c\u800c\u65e0\u6cd5\u76f4\u63a5\u626b\u63cf\u81f3 map\u3002\u4ee5\u4e0b\u793a\u4f8b\u4f1a\u63d0\u793a\u626b\u63cf\u76ee\u6807\u4e0d\u662f\u6307\u9488\u7684\u9519\u8bef\uff1a,\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5bf9 map \u8fdb\u884c\u521d\u59cb\u5316\u4ee5\u63d0\u4f9b\u6307\u9488\uff1a,\u8fd9\u6837\uff0cres \u4e2d\u7684\u6bcf\u4e00\u4e2a\u503c\u90fd\u5c06\u6210\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u5411\u5b9e\u9645\u6570\u636e\u3002\u7136\u540e\uff0cdb.queryrow(sql).scan \u5c31\u53ef\u4ee5\u5c06\u7ed3\u679c\u96c6\u626b\u63cf\u81f3 res \u4e2d\u3002,\u4fee\u6539\u540e\u7684\u793a\u4f8b\uff1a,\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01,\u5229\u7528 db.queryrow(sql).scan \u5c06\u7ed3\u679c\u96c6\u626b\u63cf\u81f3 map,\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f<\/span><\/p>\n<h1>\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f<\/h1>\n<p><span>2024-11-07 18:28:04<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662fGOLANG\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f\u300b<\/span>\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241107\/1730975268672c9624487a3.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5229\u7528 db.queryrow(sql).scan \u5c06\u7ed3\u679c\u96c6\u626b\u63cf\u81f3 map<\/strong><\/p>\n<p>\u5728 go \u4e2d\uff0cdb.queryrow(sql).scan \u53ea\u80fd\u5c06\u7ed3\u679c\u96c6\u626b\u63cf\u81f3\u53d8\u91cf\u6307\u9488\uff0c\u800c\u65e0\u6cd5\u76f4\u63a5\u626b\u63cf\u81f3 map\u3002\u4ee5\u4e0b\u793a\u4f8b\u4f1a\u63d0\u793a\u626b\u63cf\u76ee\u6807\u4e0d\u662f\u6307\u9488\u7684\u9519\u8bef\uff1a<\/p>\n<pre>res := map[string]*interface{}{\"id\": nil, \"name\": nil, \"password\": nil, \"add_time\": nil}\n\nsql := \"select * from test where id = ?\"\nerr := db.queryrow(sql, id).scan(res[\"id\"], res[\"name\"], res[\"password\"], res[\"add_time\"])<\/pre>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5bf9 map \u8fdb\u884c\u521d\u59cb\u5316\u4ee5\u63d0\u4f9b\u6307\u9488\uff1a<\/p>\n<pre>res := map[string]*interface{}{\n    \"id\":       new(int),\n    \"name\":     new(string),\n    \"password\": new(string),\n    \"add_time\": new(int),\n}<\/pre>\n<p>\u8fd9\u6837\uff0cres \u4e2d\u7684\u6bcf\u4e00\u4e2a\u503c\u90fd\u5c06\u6210\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u5411\u5b9e\u9645\u6570\u636e\u3002\u7136\u540e\uff0cdb.queryrow(sql).scan \u5c31\u53ef\u4ee5\u5c06\u7ed3\u679c\u96c6\u626b\u63cf\u81f3 res \u4e2d\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>func selectOne(id int) {\n    res := map[string]*interface{}{\n        \"id\":       new(int),\n        \"name\":     new(string),\n        \"password\": new(string),\n        \"add_time\": new(int),\n    }\n\n    sql := \"select * from test where id = ?\"\n    err := db.QueryRow(sql, id).Scan(res[\"id\"], res[\"name\"], res[\"password\"], res[\"add_time\"])\n\n    if err != nil {\n        fmt.Println(\"\u83b7\u53d6\u6570\u636e\u5931\u8d25\uff1a\", err.Error())\n    } else {\n        fmt.Println(res)\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5c06 db.QueryRow(sql).Scan \u7ed3\u679c\u626b\u63cf\u81f3 map\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728GOLANG\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Mac \u4e0a\u8fd0\u884c `go run main.go` \u9891\u9891\u5f39\u51fa\u8b66\u544a\uff1f\u5982\u4f55\u89e3\u51b3\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Python \u4e2d\u7684 unittest \u6846\u67b6\u6d4b\u8bd5\u65f6\u9047\u5230 AttributeError \u5f02\u5e38\u600e\u4e48\u529e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206259","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=206259"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206259\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}