{"id":42143,"date":"2024-12-01T10:02:09","date_gmt":"2024-12-01T02:02:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42143\/"},"modified":"2024-12-01T10:02:09","modified_gmt":"2024-12-01T02:02:09","slug":"%e5%9c%a8-golang-%e4%b8%ad%e8%af%bb%e5%8f%96%e5%92%8c%e8%a7%a3%e7%bb%84-api-%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42143\/","title":{"rendered":"\u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c"},"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>\u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c<\/span><\/p>\n<h1>\u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 19:48:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4ece api \u4e2d\u63d0\u53d6\u4e00\u4e9b\u6570\u636e\u3002<\/p>\n<p>\u5b83\u8f93\u51fa\u76f8\u5f53\u590d\u6742\u7684\u6570\u636e\u3002 \u5f53\u6211 <code>ioutil.readall(resp.body)<\/code> \u65f6\uff0c\u7ed3\u679c\u7684\u7c7b\u578b\u4e3a <code>[]uint8<\/code>\u3002 \u5982\u679c\u6211\u5c1d\u8bd5\u8bfb\u53d6\u7ed3\u679c\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u968f\u673a\u6574\u6570\u6570\u7ec4\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528 <code>string(diskinfo)<\/code> \u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6211\u5c31\u53ef\u4ee5\u8bfb\u53d6\u5b83 \u4f46\u6211\u60f3\u5728\u7ed3\u6784\u4e2d\u4f7f\u7528\u5b83\u5e76\u4e14\u5728\u89e3\u7ec4\u65f6\u9047\u5230\u95ee\u9898\u3002<\/p>\n<pre>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"io\/ioutil\"\n    \"net\/http\"\n    \"net\/url\"\n    \"reflect\"\n)\n\ntype apiresults struct {\n    results []struct {\n        statement_id int `json.statement_id`\n        series       []struct {\n            name string `json.name`\n            tags struct {\n                host string `json.host`\n            }\n            columns []string      `json.columns`\n            values  []interface{} `json.values`\n        }\n    }\n}\n\nfunc main() {\n    my_url := \"my_url\"\n    my_qry := fmt.sprintf(\"my_query\")\n\n    resp, err := http.get(my_url + url.queryescape(my_qry))\n    if err != nil {\n        fmt.printf(\"error:  %v\\n\", err)\n    } else {\n        fmt.println(reflect.typeof(resp))\n        diskinfo, _ := ioutil.readall(resp.body)\n        fmt.println(reflect.typeof((diskinfo)))\n        fmt.println(diskinfo)\n        fmt.println(string(diskinfo))\n        diskinfo_string := string(diskinfo)\n        data := apiresults{}\n        json.unmarshal([]byte(diskinfo_string), &amp;data)\n        \/\/fmt.printf(\"values = %v\\n\", data.results.series.values)\n        \/\/fmt.printf(\"server = %v\\n\", data.results.series.tags.host)\n    }\n}<\/pre>\n<p>\u5982\u679c\u6211\u5c06\u6570\u636e\u89c6\u4e3a\u5b57\u7b26\u4e32\uff0c\u6211\u4f1a\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c\uff08\u683c\u5f0f\u5316\uff09\uff1a<\/p>\n<pre>{\"results\":[\n  {\"statement_id\":0,\n   \"series\":[\n    {\"name\":\"disk\",\n     \"tags\":{\"host\":\"myServer1\"},\n     \"columns\":[\"time\",\"disk_size\"],\n     \"values\":[[\"2021-07-07T07:53:32.291490387Z\",1044]]},\n    {\"name\":\"disk\",\n     \"tags\":{\"host\":\"myServer2\"},\n     \"columns\":[\"time\",\"disk_size\"],\n     \"values\":[[\"2021-07-07T07:53:32.291490387Z\",1046]]}\n  ]}\n]}<\/pre>\n<p>\u6211\u8ba4\u4e3a\u6211\u7684 <code>apireturn<\/code> \u7ed3\u6784\u7684\u7ed3\u6784\u4e5f\u4e0d\u6b63\u786e\uff0c\u56e0\u4e3a api \u7ed3\u679c\u5305\u542b\u591a\u4e2a\u4e3b\u673a\u7684\u4fe1\u606f\u3002 \u4f46\u9996\u5148\uff0c\u6211\u6000\u7591\u6570\u636e\u662f\u5426\u5fc5\u987b\u4ee5\u4e0d\u540c\u7684\u683c\u5f0f\u53d1\u9001\u5230\u7ed3\u6784\u3002\u4e00\u65e6\u6211\u8fd9\u6837\u505a\u4e86\uff0c\u6211\u53ef\u80fd\u4f1a\u5c1d\u8bd5\u5f04\u6e05\u695a\u4e0b\u4e00\u6b65\u5982\u4f55\u4ece\u7ed3\u6784\u4e2d\u8bfb\u53d6\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p><code>ioutil.readall<\/code> \u5df2\u4e3a\u60a8\u63d0\u4f9b <code>byte[]<\/code> \u7c7b\u578b\u7684\u6570\u636e\u3002\u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u8c03\u7528 <code>json.unmarshal<\/code> \u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u3002<\/p>\n<pre>import (\n    \"encoding\/json\"\n    \"io\/ioutil\"\n    \"net\/http\"\n)\n\n\nfunc tostruct(res *http.response) (*apiresults, error) {\n    body, err := ioutil.readall(res.body)\n    if err != nil {\n        return nil, err\n    }\n\n    defer res.body.close()\n\n    data := apiresults{}\n    if err := json.unmarshal(body, &amp;data); err != nil {\n        return nil, err\n    }\n\n    return data, nil \n}\n<\/pre>\n<p>\u60a8\u7684\u7ed3\u6784\u4f3c\u4e4e\u4e5f\u5b58\u5728\u95ee\u9898\u3002 struct\u6807\u7b7e\u7684\u6b63\u786e\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\u3002\u53e6\u5916\uff0c\u9700\u8981\u5bfc\u51fa\u5b57\u6bb5\u624d\u80fd\u4f7f json \u6807\u8bb0\uff08\u7531 <code>json.umarshal<\/code> \u4f7f\u7528\uff09\u6b63\u5e38\u5de5\u4f5c &#8211; \u4ece\u5927\u5199\u5b57\u6bcd\u5f00\u59cb\u5373\u53ef\u3002<\/p>\n<pre>type ApiResults struct {\n    Results []struct {\n        StatementId int `json:\"statement_id\"`\n        Series       []struct {\n            Name string `json:\"name\"`\n            Tags struct {\n                Host string `json:\"host\"`\n            } `json:\"tags\"`\n            Columns []string      `json:\"columns\"`\n            Values  []interface{} `json:\"values\"`\n        } `json:\"series\"`\n    } `json:\"results\"`\n}\n<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c \u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c \u6765\u6e90\uff1astackoverflow 2024-04-22 19:48:37 0\u6d4f\u89c8 \u6536\u85cf \u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8eGolang\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587\u300a\u5728 Golang \u4e2d\u8bfb\u53d6\u548c\u89e3\u7ec4 API \u7ed3\u679c\u300b\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60Golang\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~ \u95ee\u9898\u5185\u5bb9 \u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4ece api \u4e2d\u63d0\u53d6\u4e00\u4e9b\u6570\u636e\u3002 \u5b83\u8f93\u51fa\u76f8\u5f53\u590d\u6742\u7684\u6570\u636e\u3002 \u5f53\u6211 ioutil.readall(resp.body) \u65f6\uff0c\u7ed3\u679c\u7684\u7c7b\u578b\u4e3a []uint8\u3002 \u5982\u679c\u6211\u5c1d\u8bd5\u8bfb\u53d6\u7ed3\u679c\uff0c\u5b83\u53ea\u662f\u4e00\u4e2a\u968f\u673a\u6574\u6570\u6570\u7ec4\u3002 \u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528 string(diskinfo) \u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6211\u5c31\u53ef\u4ee5\u8bfb\u53d6\u5b83 \u4f46\u6211\u60f3\u5728\u7ed3\u6784\u4e2d\u4f7f\u7528\u5b83\u5e76\u4e14\u5728\u89e3\u7ec4\u65f6\u9047\u5230\u95ee\u9898\u3002 package main import ( &#8220;encoding\/json&#8221; &#8220;fmt&#8221; &#8220;io\/ioutil&#8221; &#8220;net\/http&#8221; &#8220;net\/url&#8221; &#8220;reflect&#8221; ) type apiresults struct { results [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42143","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42143"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42143\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}