{"id":1242,"date":"2022-11-28T09:16:07","date_gmt":"2022-11-28T01:16:07","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1242"},"modified":"2022-12-05T11:26:31","modified_gmt":"2022-12-05T03:26:31","slug":"go%e6%8c%87%e5%8d%97%e7%bb%83%e4%b9%a05-stringer","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1242","title":{"rendered":"Go\u6307\u5357\u7ec3\u4e605-Stringer"},"content":{"rendered":"\n<p>\u8fd9\u4e2a\u662f<a rel=\"noreferrer noopener\" href=\"https:\/\/go.dev\/tour\/list\" target=\"_blank\">A Tour of Go<\/a>\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u4e94\u4e2a\u7ec3\u4e60\uff0c<a rel=\"noreferrer noopener\" href=\"https:\/\/tour.go-zh.org\/methods\/18\" target=\"_blank\">\u539f\u9898<\/a>\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u901a\u8fc7\u8ba9&nbsp;<code>IPAddr<\/code>&nbsp;\u7c7b\u578b\u5b9e\u73b0&nbsp;<code>fmt.Stringer<\/code>&nbsp;\u6765\u6253\u5370\u70b9\u53f7\u5206\u9694\u7684\u5730\u5740\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c<code>IPAddr{1, 2, 3, 4}<\/code>&nbsp;\u5e94\u5f53\u6253\u5370\u4e3a&nbsp;<code>\"1.2.3.4\"<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<p>\u539f\u9898\u6a21\u677f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype IPAddr &#91;4]byte\n\n\/\/ TODO: Add a \"String() string\" method to IPAddr.\n\nfunc main() {\n\thosts := map&#91;string]IPAddr{\n\t\t\"loopback\":  {127, 0, 0, 1},\n\t\t\"googleDNS\": {8, 8, 8, 8},\n\t}\n\tfor name, ip := range hosts {\n\t\tfmt.Printf(\"%v: %v\\n\", name, ip)\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>loopback: &#91;127 0 0 1]\ngoogleDNS: &#91;8 8 8 8]<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u73b0\u5728\u8981\u505a\u7684\u5c31\u662f\u628a\u8fd9\u4e2a\u5207\u7247\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7528\u70b9\u8fde\u63a5\u8d77\u6765\u7684\u5b57\u7b26\u4e32\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype IPAddr &#91;4]byte\n\n\/\/ TODO: \u7ed9 IPAddr \u6dfb\u52a0\u4e00\u4e2a \"String() string\" \u65b9\u6cd5\n\nfunc (ip IPAddr) String() string {\n\treturn fmt.Sprintf(\"%v.%v.%v.%v\", ip&#91;0], ip&#91;1], ip&#91;2], ip&#91;3])\n}\n\nfunc main() {\n\thosts := map&#91;string]IPAddr{\n\t\t\"loopback\":  {127, 0, 0, 1},\n\t\t\"googleDNS\": {8, 8, 8, 8},\n\t}\n\tfor name, ip := range hosts {\n\t\tfmt.Printf(\"%v: %v\\n\", name, ip)\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u540e\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>loopback: 127.0.0.1\ngoogleDNS: 8.8.8.8<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u662fA Tour of Go\u8fd9\u4e2a\u6559\u7a0b\u91cc\u9762\u7684\u7b2c\u4e94\u4e2a\u7ec3\u4e60\uff0c\u539f\u9898\u5982\u4e0b\uff1a \u901a\u8fc7\u8ba9&nbsp;IPAddr&nbsp;\u7c7b\u578b\u5b9e\u73b0&nbsp;fmt.Stringer&nbsp;\u6765\u6253\u5370\u70b9\u53f7\u5206\u9694\u7684\u5730\u5740\u3002 \u4f8b\u5982\uff0cIPAddr{1, 2, 3, 4}&nbsp;\u5e94\u5f53\u6253\u5370\u4e3a&nbsp;&#8220;1.2.3.4&#8221;\u3002 \u539f\u9898\u6a21\u677f\u5982\u4e0b\uff1a \u8fd9\u4e2a\u7a0b\u5e8f\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\uff0c\u7ed3\u679c\u5982\u4e0b\uff1a \u6211\u4eec\u73b0\u5728\u8981\u505a\u7684\u5c31\u662f\u628a\u8fd9\u4e2a\u5207\u7247\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7528\u70b9\u8fde\u63a5\u8d77\u6765\u7684\u5b57\u7b26\u4e32\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a \u6267\u884c\u540e\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[374],"class_list":["post-1242","post","type-post","status-publish","format-standard","hentry","category-coding","tag-go"],"views":293,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=1242"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1242\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}