{"id":15551,"date":"2024-11-18T20:15:26","date_gmt":"2024-11-18T12:15:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15551"},"modified":"2024-11-18T20:15:26","modified_gmt":"2024-11-18T12:15:26","slug":"go%e6%96%b0%e6%89%8b%e9%9c%80%e6%b3%a8%e6%84%8f%e7%9a%84%e7%9f%a5%e8%af%86%e7%82%b9%e4%ba%8c","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15551\/","title":{"rendered":"Go\u65b0\u624b\u9700\u6ce8\u610f\u7684\u77e5\u8bc6\u70b9(\u4e8c)"},"content":{"rendered":"<p>\u5b66\u4e60Go\u7684\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u77e5\u8bc6\u70b9\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>\u4e03\u3001\u6570\u7ec4<\/strong><\/h3>\n<p><strong>1\u3001<\/strong>\u6570\u7ec4\u7533\u660e\uff1avar identifier [len]type ; arr := [\u2026]string{\u201caa\u201d,\u201dbb\u201d,\u201dcc\u201d,\u201ddd\u201d}<\/p>\n<p><strong>2\u3001<\/strong>\u4f7f\u7528for\u3001for-range\u5faa\u73af\u6253\u5370\u6570\u7ec4, for k,v := range arr {} ; for i := 0; i &lt; len(arr); i++ {}<\/p>\n<p><strong>3\u3001<\/strong>\u6570\u7ec4\u548c\u5207\u7247\u7684\u533a\u522b\u3002\u53c2\u8003\uff1aGO\u4e2d\u6570\u7ec4\u4e0e\u5207\u7247\u7684\u533a\u522b<\/p>\n<p><strong>4\u3001<\/strong>\u7ec8\u6b62\u7d22\u5f15\u6807\u8bc6\u7684\u9879\u4e0d\u5305\u62ec\u5728\u5207\u7247\u5185<\/p>\n<h3><strong>\u516b\u3001map<\/strong><\/h3>\n<p><strong>1\u3001<\/strong>map\u7684\u58f0\u660e<\/p>\n<ul>\n<li>var identifier map [keyType] valueType<\/li>\n<li>var identifier = make(map [keyType] valueType)<\/li>\n<li>identifier :=\u00a0make(map [keyType] valueType)<\/li>\n<\/ul>\n<p><strong>2\u3001<\/strong>valueType\u7684\u5f62\u5f0f\u591a\u6837\u3002\u51fa\u4e86string\u3001int\uff0c\u8fd8\u53ef\u4ee5\u5305\u62ec[4]int\u3001[]int\u3001func() int \u3001map [keyType] valueType\u7b49\u3002<\/p>\n<p><strong>3\u3001\u6570\u7ec4\u3001\u5207\u7247\u3001map\uff0c\u8fd9\u4e09\u79cd\u7c7b\u578b\u4e2d\uff0cmap\u7684\u6027\u80fd\u662f\u6700\u4f4e\u7684\uff0c\u8bfb\u53d6\u7684\u6027\u80fd\u6bd4\u4f8b\u5927\u6982\u662f\u00a01\u00a0:\u00a01.5\u00a0:\u00a025<\/strong><\/p>\n<p><strong>4\u3001<\/strong>\u6ce8\u610f\uff1amap \u9ed8\u8ba4\u662f\u65e0\u5e8f\u7684\uff0c\u4e0d\u662f\u6309\u7167 key \u7684\u987a\u5e8f\u6392\u5217\u7684\uff0c\u4e5f\u4e0d\u662f\u6309\u7167 value \u7684\u5e8f\u6392\u5217\u7684\u3002\u6bcf\u6b21\u5237\u65b0\u540e\uff0c\u6392\u5e8f\u4f1a\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<p><strong>5\u3001<\/strong>\u4f7f\u7528val1, isPresent = map1[key1] \u65b9\u6cd5\uff0c\u6765\u5224\u65adkey1\u662f\u5426\u5b58\u5728\uff1f<\/p>\n<p><strong>6\u3001<\/strong>map\u7684\u6392\u5e8f\u3001key\u548c\u503c\u4e92\u6362\u7684\u65b9\u6cd5\u3002<\/p>\n<h3><strong>\u4e5d\u3001\u5305<\/strong><\/h3>\n<p><strong>1\u3001<\/strong>math\/big\uff0c\u53ef\u4ee5\u5bf9\u5927\u7684\u6574\u6570\u8fdb\u884c\u8fd0\u7b97\uff0c\u4f46\u6027\u80fd\u6781\u5176\u4f4e\u4e0b\u3002<\/p>\n<p>2\u3001\u53c2\u8003\uff1a<strong>Go\u4e2dimport \u5bfc\u5165\u5305\u7684\u8bed\u6cd5<\/strong><\/p>\n<h3><strong>\u5341\u3001\u7ed3\u6784\u4f53 struct<\/strong><\/h3>\n<p><strong>1\u3001<\/strong>\u7ed3\u6784\u4f53\u7684\u5b9a\u4e49\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre>type identifier struct {\r\n\u00a0\u00a0\u00a0 field1 type1\r\n\u00a0\u00a0\u00a0 field2 type2\r\n\u00a0\u00a0\u00a0 ...\r\n}<\/pre>\n<p><strong>2\u3001<\/strong>\u7ed3\u6784\u7684\u58f0\u660e\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<pre>\u7b2c\u4e00\u79cd\uff1a\r\nvar t *T\r\nt = new(T)\r\n\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5199\u6210\uff1a t := new(T)\r\n\r\n\u7b2c\u4e8c\u79cd\uff1a\r\nvar t T<\/pre>\n<p>\u6ce8\u610f\u8fd92\u79cd\u7684\u533a\u522b\uff0c\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff1a\u53d8\u91cf t \u662f\u4e00\u4e2a\u6307\u5411 T\u7684\u6307\u9488\uff0c\u6b64\u65f6\u7ed3\u6784\u4f53\u5b57\u6bb5\u7684\u503c\u662f\u5b83\u4eec\u6240\u5c5e\u7c7b\u578b\u7684\u96f6\u503c\u3002<br \/>\n\u58f0\u660e var t T \u4e5f\u4f1a\u7ed9 t \u5206\u914d\u5185\u5b58\uff0c\u5e76\u96f6\u503c\u5316\u5185\u5b58\uff0c\u4f46\u662f\u8fd9\u4e2a\u65f6\u5019 t \u662f\u7c7b\u578bT\u3002\u5728\u8fd9\u4e24\u79cd\u65b9\u5f0f\u4e2d\uff0ct \u901a\u5e38\u88ab\u79f0\u505a\u7c7b\u578b T \u7684\u4e00\u4e2a\u5b9e\u4f8b\uff08instance\uff09\u6216\u5bf9\u8c61\uff08object\uff09\u3002<\/p>\n<p>\u65e0\u8bba\u53d8\u91cft\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u8fd8\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u6307\u9488\uff0c\u90fd\u4f7f\u7528\u540c\u6837\u7684 \u9009\u62e9\u5668\u7b26\uff08selector-notation\uff09 \u6765\u5f15\u7528\u7ed3\u6784\u4f53\u7684\u5b57\u6bb5\uff1a<\/p>\n<pre>type myStruct struct { i int }\r\nvar v myStruct\u00a0\u00a0\u00a0 \/\/ v\u662f\u7ed3\u6784\u4f53\u7c7b\u578b\u53d8\u91cf\r\nvar p *myStruct\u00a0\u00a0 \/\/ p\u662f\u6307\u5411\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u53d8\u91cf\u7684\u6307\u9488\r\nv.i\r\np.i<\/pre>\n<p><strong>\u00a03\u3001<\/strong>\u7ed3\u6784\u4f53\u521d\u59cb\u5316<\/p>\n<pre>type struct1 struct {\r\n\u00a0\u00a0\u00a0 i1\u00a0 int\r\n\u00a0\u00a0\u00a0 f1\u00a0 float32\r\n\u00a0\u00a0\u00a0 str string\r\n}<\/pre>\n<ul>\n<li>ms := &amp;struct1{10, 15.5, \u201cChris\u201d}<\/li>\n<li>ms := struct1{10, 15.5, \u201cChris\u201d}<\/li>\n<\/ul>\n<p><strong>\u00a04\u3001<\/strong>\u7ed3\u6784\u4f53\u4e2d\u7684\u65b9\u6cd5<\/p>\n<p>\u53c2\u8003\uff1aGO\u4e2d\u7ed3\u6784\u4f53 struct \u4e2d\u7684\u65b9\u6cd5<\/p>\n<h3><strong>\u5341\u4e00\u3001\u63a5\u53e3<\/strong><\/h3>\n<h3><strong>1\u3001\u63a5\u53e3\u5b9a\u4e49\u683c\u5f0f<\/strong><\/h3>\n<pre>type Namer interface {\r\n\u00a0\u00a0\u00a0 Method1(param_list) return_type\r\n\u00a0\u00a0\u00a0 Method2(param_list) return_type\r\n\u00a0\u00a0\u00a0 ...\r\n}<\/pre>\n<h3><strong>2\u3001\u63a5\u53e3\u547d\u540d\u7684\u65b9\u5f0f<\/strong><\/h3>\n<p>\u63a5\u53e3\u7684\u540d\u5b57\u7531\u65b9\u6cd5\u540d\u52a0 [e]r \u540e\u7f00\u7ec4\u6210\uff0c\u4f8b\u5982 Printer\u3001Reader\u3001Writer\u3001Logger\u3001Converter \u7b49\u7b49\u3002\u8fd8\u6709\u4e00\u4e9b\u4e0d\u5e38\u7528\u7684\u65b9\u5f0f\uff08\u5f53\u540e\u7f00 er \u4e0d\u5408\u9002\u65f6\uff09\uff0c\u6bd4\u5982 Recoverable\uff0c\u6b64\u65f6\u63a5\u53e3\u540d\u4ee5 able \u7ed3\u5c3e\uff0c\u6216\u8005\u4ee5 I \u5f00\u5934\uff08\u50cf .NET \u6216 Java \u4e2d\u90a3\u6837\uff09\u3002<br \/>\nGo \u8bed\u8a00\u4e2d\u7684\u63a5\u53e3\u90fd\u5f88\u7b80\u77ed\uff0c\u901a\u5e38\u5b83\u4eec\u4f1a\u5305\u542b 0 \u4e2a\u3001\u6700\u591a 3 \u4e2a\u65b9\u6cd5\u3002<\/p>\n<h3><strong>3\u3001\u63a5\u53e3\u7c7b\u578b\u7684\u5224\u65ad\uff1atype-switch<\/strong><\/h3>\n<pre>switch t := o.(type) {\r\n\u00a0\u00a0\u00a0 case *stockPostion:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fmt.Printf(\"Type Square %T with value %v\\n\", t, t)\r\n\u00a0\u00a0\u00a0 case *car:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fmt.Printf(\"Type Circle %T with value %v\\n\", t, t)\r\n\u00a0\u00a0\u00a0 case nil:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fmt.Printf(\"nil value: nothing to check?\\n\")\r\n\u00a0\u00a0\u00a0 default:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fmt.Printf(\"Unexpected type %T\\n\", t)\r\n\u00a0\u00a0\u00a0 }<\/pre>\n<p><strong>4\u3001\u5728\u63a5\u53e3\u4e0a\u8c03\u7528\u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u6709\u548c\u65b9\u6cd5\u5b9a\u4e49\u65f6\u76f8\u540c\u7684\u63a5\u6536\u8005\u7c7b\u578b\u6216\u8005\u662f\u53ef\u4ee5\u4ece\u5177\u4f53\u7c7b\u578b P \u76f4\u63a5\u53ef\u4ee5\u8fa8\u8bc6\u7684\uff1a<\/strong><\/p>\n<ul>\n<li>\u6307\u9488\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8c03\u7528<\/li>\n<li>\u503c\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u503c\u8c03\u7528<\/li>\n<li>\u63a5\u6536\u8005\u662f\u503c\u7684\u65b9\u6cd5\u53ef\u4ee5\u901a\u8fc7\u6307\u9488\u8c03\u7528\uff0c\u56e0\u4e3a\u6307\u9488\u4f1a\u9996\u5148\u88ab\u89e3\u5f15\u7528<\/li>\n<li>\u63a5\u6536\u8005\u662f\u6307\u9488\u7684\u65b9\u6cd5\u4e0d\u53ef\u4ee5\u901a\u8fc7\u503c\u8c03\u7528\uff0c\u56e0\u4e3a\u5b58\u50a8\u5728\u63a5\u53e3\u4e2d\u7684\u503c\u6ca1\u6709\u5730\u5740<\/li>\n<\/ul>\n<p>\u5c06\u4e00\u4e2a\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u63a5\u53e3\u65f6\uff0c\u7f16\u8bd1\u5668\u4f1a\u786e\u4fdd\u6240\u6709\u53ef\u80fd\u7684\u63a5\u53e3\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5728\u6b64\u503c\u4e0a\u88ab\u8c03\u7528\uff0c\u56e0\u6b64\u4e0d\u6b63\u786e\u7684\u8d4b\u503c\u5728\u7f16\u8bd1\u671f\u5c31\u4f1a\u5931\u8d25\u3002<\/p>\n<p><strong>\u8bd1\u6ce8\uff1aGo \u8bed\u8a00\u89c4\u8303\u5b9a\u4e49\u4e86\u63a5\u53e3\u65b9\u6cd5\u96c6\u7684\u8c03\u7528\u89c4\u5219\uff1a<\/strong><\/p>\n<ul>\n<li>\u7c7b\u578b *T \u7684\u53ef\u8c03\u7528\u65b9\u6cd5\u96c6\u5305\u542b\u63a5\u53d7\u8005\u4e3a *T \u6216 T \u7684\u6240\u6709\u65b9\u6cd5\u96c6<\/li>\n<li>\u7c7b\u578b T \u7684\u53ef\u8c03\u7528\u65b9\u6cd5\u96c6\u5305\u542b\u63a5\u53d7\u8005\u4e3a T \u7684\u6240\u6709\u65b9\u6cd5<\/li>\n<li>\u7c7b\u578b T \u7684\u53ef\u8c03\u7528\u65b9\u6cd5\u96c6\u4e0d\u5305\u542b\u63a5\u53d7\u8005\u4e3a *T \u7684\u65b9\u6cd5<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5b66\u4e60Go\u7684\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u77e5\u8bc6\u70b9\u3002 &nbsp; \u4e03\u3001\u6570\u7ec4 1\u3001\u6570\u7ec4\u7533\u660e\uff1avar identifier [len]type ; arr := [\u2026]string{\u201caa\u201d,\u201dbb\u201d,\u201dcc\u201d,\u201ddd\u201d} 2\u3001\u4f7f\u7528for\u3001for-range\u5faa\u73af\u6253\u5370\u6570\u7ec4, for k,v := range arr {} ; for i := 0; i &lt; len(arr); i++ {} 3\u3001\u6570\u7ec4\u548c\u5207\u7247\u7684\u533a\u522b\u3002\u53c2\u8003\uff1aGO\u4e2d\u6570\u7ec4\u4e0e\u5207\u7247\u7684\u533a\u522b 4\u3001\u7ec8\u6b62\u7d22\u5f15\u6807\u8bc6\u7684\u9879\u4e0d\u5305\u62ec\u5728\u5207\u7247\u5185 \u516b\u3001map 1\u3001map\u7684\u58f0\u660e var identifier map [keyType] valueType var identifier = make(map [keyType] valueType) identifier :=\u00a0make(map [keyType] valueType) 2\u3001valueType\u7684\u5f62\u5f0f\u591a\u6837\u3002\u51fa\u4e86string\u3001int\uff0c\u8fd8\u53ef\u4ee5\u5305\u62ec[4]int\u3001[]int\u3001func() int \u3001map [keyType] valueType\u7b49\u3002 3\u3001\u6570\u7ec4\u3001\u5207\u7247\u3001map\uff0c\u8fd9\u4e09\u79cd\u7c7b\u578b\u4e2d\uff0cmap\u7684\u6027\u80fd\u662f\u6700\u4f4e\u7684\uff0c\u8bfb\u53d6\u7684\u6027\u80fd\u6bd4\u4f8b\u5927\u6982\u662f\u00a01\u00a0:\u00a01.5\u00a0:\u00a025 4\u3001\u6ce8\u610f\uff1amap \u9ed8\u8ba4\u662f\u65e0\u5e8f\u7684\uff0c\u4e0d\u662f\u6309\u7167 key [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-15551","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15551","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=15551"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15551\/revisions"}],"predecessor-version":[{"id":15552,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15551\/revisions\/15552"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}