{"id":207071,"date":"2025-07-08T08:59:40","date_gmt":"2025-07-08T00:59:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207071\/"},"modified":"2025-07-08T08:59:40","modified_gmt":"2025-07-08T00:59:40","slug":"%e5%b0%86select-%e5%88%97%ef%bc%88%e5%a4%9a%e4%b8%aa%ef%bc%89%e8%af%bb%e5%8f%96%e5%88%b0-go-%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207071\/","title":{"rendered":"\u5c06\u201cSELECT *\u201d\u5217\uff08\u591a\u4e2a\uff09\u8bfb\u53d6\u5230 go \u4e2d\u7684\u5b57\u7b26\u4e32\u4e2d"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5c06\u201cSELECT *\u201d\u5217\uff08\u591a\u4e2a\uff09\u8bfb\u53d6\u5230 go \u4e2d\u7684\u5b57\u7b26\u4e32\u4e2d<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 20:42:24<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5c06\u201cSELECT *\u201d\u5217\uff08\u591a\u4e2a\uff09\u8bfb\u53d6\u5230 go \u4e2d\u7684\u5b57\u7b26\u4e32\u4e2d\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u60f3\u5c06 mysql \u6570\u636e\u5e93\u5217\u63d2\u5165\u5230 go \u4e2d\u7684 [][]string \u4e2d\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u7c7b\u4f3c\u7684\u4ee3\u7801\uff0c\u5b83\u4ec5\u5bf9\u4e00\u5217\u6267\u884c\u6b64\u64cd\u4f5c\u5e76\u5c06\u5176\u63d2\u5165\u5230 []string \u4e2d\uff0c\u4f46\u6211\u9700\u8981\u5c06\u66f4\u591a\u5217\u63d2\u5165\u5230 [][]string \u4e2d\u5236\u4f5c\u4e00\u4e2a\u6570\u636e\u6846\u3002<\/p>\n<pre>mysql&gt; select * from users;\n+----+-----------+----------+----------+-------------------------------+--------------+\n| id | fname     | lname    | uname    | email                         | contact      |\n+----+-----------+----------+----------+-------------------------------+--------------+\n|  1 | rishikesh | agrawani | hygull   | [email&nbsp;protected] | 917353787704 |\n|  2 | sandeep   | e        | sandeep  | [email&nbsp;protected]       | 919739040038 |\n|  3 | darshan   | sidar    | darshan  | [email&nbsp;protected]        | 917996917565 |\n|  4 | surendra  | prajapat | surendra | [email&nbsp;protected]     | 918385894407 |\n|  5 | mukesh    | jakhar   | mukesh   | [email&nbsp;protected]     | 919772254140 |\n+----+-----------+----------+----------+-------------------------------+--------------+\n5 rows in set (0.00 sec)\n\nmysql&gt; \n<\/pre>\n<pre>func main() {\n    \/\/ db, err := sql.Open(\"mysql\", \"&lt;username&gt;:&lt;password&gt;@tcp(127.0.0.1:&lt;port&gt;)\/&lt;dbname&gt;?charset=utf8\" )\n    db, err := sql.Open(\"mysql\", \"hygull:admin@67@tcp(127.0.0.1:3306)\/practice_db?charset=utf8\")\n\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    rows, err := db.Query(\"select fname from users\")\n\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    firstnames:=[]string{}\n    for rows.Next() {\n        var fname string\n        rows.Scan(&amp;fname)\n        firstnames = append(firstnames, fname)\n    }\n\n    fmt.Println(firstnames)\n    db.Close()\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5047\u8bbe\u60a8\u7684\u67e5\u8be2\u8fd4\u56de 4 \u5217\uff0c\u60a8\u53ea\u9700\u4e00\u6b21\u626b\u63cf\u591a\u4e2a\u503c\u3002<\/p>\n<pre>details:=[][]string{}\nfor rows.next() {\n    cols := make([]string, 4) \n    rows.scan(&amp;cols[0], &amp;cols[1], &amp;cols[2], &amp;cols[3])\n    details = append(details, cols)\n}<\/pre>\n<p>\u5982\u679c\u5217\u6570\u4e0d\u662f\u9759\u6001\u7684\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u6765\u81ea\u52a8\u5316\u5b83\uff0c\u800c\u4e0d\u662f\u624b\u52a8\u6269\u5c55\u6307\u9488\uff1a<\/p>\n<pre>details:=[][]string{}\nfor rows.Next() {\n    cols := make([]string, num_cols)\n    \/\/ Create an array of string pointers pointing to the column strings.\n    col_ptrs = make([]*string, num_cols)\n    for i := 0; i &lt; num_cols; i++ {\n        col_ptrs[i] = &amp;cols[i];\n    }\n    rows.Scan(col_ptrs...)\n    details = append(details, cols)\n}<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207071","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207071","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=207071"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207071\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}