{"id":3204,"date":"2025-03-25T23:57:19","date_gmt":"2025-03-25T15:57:19","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3204"},"modified":"2025-03-25T23:57:19","modified_gmt":"2025-03-25T15:57:19","slug":"go-%e8%af%ad%e8%a8%80%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/25\/go-%e8%af%ad%e8%a8%80%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2\/","title":{"rendered":"Go \u8bed\u8a00\u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"\n<p>Go \u8bed\u8a00\u662f\u4e00\u79cd\u5f3a\u7c7b\u578b\u8bed\u8a00\uff0c\u7c7b\u578b\u8f6c\u6362\u662f\u6307\u5c06\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210\u53e6\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u3002Go \u4e2d\u6709\u4e24\u79cd\u7c7b\u578b\u8f6c\u6362\uff1a<strong>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong>\u548c<strong>\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a\u9700\u8981\u901a\u8fc7\u663e\u5f0f\u7684\u8bed\u6cd5\u6765\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong>\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1aGo \u8bed\u8a00\u5e76\u4e0d\u652f\u6301\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u6240\u6709\u7c7b\u578b\u8f6c\u6362\u90fd\u5fc5\u987b\u663e\u5f0f\u8fdb\u884c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u663e\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong><\/h2>\n\n\n\n<p>\u5728 Go \u4e2d\uff0c\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362\u662f\u901a\u8fc7 <code>type(value)<\/code> \u8bed\u6cd5\u6765\u5b9e\u73b0\u7684\uff0c<code>type<\/code> \u662f\u76ee\u6807\u7c7b\u578b\uff0c<code>value<\/code> \u662f\u539f\u59cb\u503c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u57fa\u672c\u7c7b\u578b\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<p>\u57fa\u672c\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u3001\u5b57\u7b26\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u6574\u6570\u4e0e\u6d6e\u70b9\u6570\u4e4b\u95f4\u7684\u8f6c\u6362<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var a int = 10\n    var b float64 = float64(a)  \/\/ \u663e\u5f0f\u8f6c\u6362 int \u5230 float64\n    fmt.Println(b)  \/\/ \u8f93\u51fa: 10.0\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u6d6e\u70b9\u6570\u5230\u6574\u6570\u7684\u8f6c\u6362<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var a float64 = 9.99\n    var b int = int(a)  \/\/ \u663e\u5f0f\u8f6c\u6362 float64 \u5230 int\uff08\u4f1a\u622a\u65ad\u5c0f\u6570\u90e8\u5206\uff09\n    fmt.Println(b)  \/\/ \u8f93\u51fa: 9\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5b57\u7b26\u4e0e\u6570\u5b57\u4e4b\u95f4\u7684\u8f6c\u6362<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    var char byte = 'A'  \/\/ \u5b57\u7b26 'A' \u5bf9\u5e94 ASCII \u7801 65\n    var num int = int(char)  \/\/ \u5c06\u5b57\u7b26\u8f6c\u6362\u4e3a\u5176\u5bf9\u5e94\u7684 ASCII \u6570\u5b57\n    fmt.Println(num)  \/\/ \u8f93\u51fa: 65\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u5e03\u5c14\u503c\u4e0e\u6570\u5b57\u7684\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<p>Go \u4e0d\u652f\u6301\u76f4\u63a5\u5c06\u5e03\u5c14\u503c\u4e0e\u6570\u503c\u7c7b\u578b\u76f8\u4e92\u8f6c\u6362\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u4ee5\u4e0b\u4ee3\u7801\u4f1a\u62a5\u9519\n\/\/ var b int = bool(true)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 \u5b57\u7b26\u4e32\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u8f6c\u6362<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c06 <code>int<\/code> \u6216 <code>float<\/code> \u8f6c\u6362\u4e3a <code>string<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>fmt.Sprintf<\/code> \u6216 <code>strconv<\/code> \u5305\u3002<\/li>\n\n\n\n<li>\u5c06 <code>string<\/code> \u8f6c\u6362\u4e3a\u5176\u4ed6\u7c7b\u578b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>strconv<\/code> \u5305\u63d0\u4f9b\u7684\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u6574\u6570\u8f6c\u5b57\u7b26\u4e32<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\nimport \"strconv\"\n\nfunc main() {\n    var a int = 123\n    str := strconv.Itoa(a)  \/\/ \u5c06\u6574\u6570\u8f6c\u4e3a\u5b57\u7b26\u4e32\n    fmt.Println(str)        \/\/ \u8f93\u51fa: \"123\"\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5b57\u7b26\u4e32\u8f6c\u6574\u6570<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\nimport \"strconv\"\n\nfunc main() {\n    var s string = \"456\"\n    num, err := strconv.Atoi(s)  \/\/ \u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6574\u6570\n    if err != nil {\n        fmt.Println(\"\u8f6c\u6362\u5931\u8d25:\", err)\n    } else {\n        fmt.Println(num)  \/\/ \u8f93\u51fa: 456\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u7ed3\u6784\u4f53\u4e0e\u63a5\u53e3\u7684\u7c7b\u578b\u8f6c\u6362<\/strong><\/h2>\n\n\n\n<p>Go \u652f\u6301\u5c06\u7ed3\u6784\u4f53\u7c7b\u578b\u8f6c\u6362\u4e3a\u63a5\u53e3\u7c7b\u578b\uff08\u9690\u5f0f\u5b9e\u73b0\u63a5\u53e3\uff09\u4ee5\u53ca\u5c06\u63a5\u53e3\u7c7b\u578b\u8f6c\u6362\u4e3a\u5177\u4f53\u7684\u7ed3\u6784\u4f53\u7c7b\u578b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u7ed3\u6784\u4f53\u8f6c\u6362\u4e3a\u63a5\u53e3\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>\u4e00\u4e2a\u7ed3\u6784\u4f53\u7c7b\u578b\u5982\u679c\u5b9e\u73b0\u4e86\u63a5\u53e3\u7c7b\u578b\u7684\u65b9\u6cd5\u96c6\uff0c\u5c31\u53ef\u4ee5\u9690\u5f0f\u5730\u8d4b\u503c\u7ed9\u8be5\u63a5\u53e3\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ \u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\ntype Speaker interface {\n    Speak() string\n}\n\n\/\/ \u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\ntype Person struct {\n    Name string\n}\n\n\/\/ Person \u5b9e\u73b0 Speaker \u63a5\u53e3\u7684\u65b9\u6cd5\nfunc (p Person) Speak() string {\n    return \"Hello, my name is \" + p.Name\n}\n\nfunc main() {\n    var speaker Speaker\n    person := Person{Name: \"John\"}\n    speaker = person  \/\/ \u9690\u5f0f\u8f6c\u6362\u4e3a\u63a5\u53e3\u7c7b\u578b\n    fmt.Println(speaker.Speak())  \/\/ \u8f93\u51fa: Hello, my name is John\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u63a5\u53e3\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <strong>\u7c7b\u578b\u65ad\u8a00<\/strong> \u6765\u5c06\u63a5\u53e3\u7c7b\u578b\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\u3002\u7c7b\u578b\u65ad\u8a00\u7684\u8bed\u6cd5\u4e3a\uff1a<code>x.(T)<\/code>\uff0c\u5176\u4e2d <code>x<\/code> \u662f\u63a5\u53e3\u53d8\u91cf\uff0c<code>T<\/code> \u662f\u76ee\u6807\u7c7b\u578b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u63a5\u53e3\u8f6c\u4e3a\u5177\u4f53\u7c7b\u578b<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\ntype Speaker interface {\n    Speak() string\n}\n\ntype Person struct {\n    Name string\n}\n\nfunc (p Person) Speak() string {\n    return \"Hello, my name is \" + p.Name\n}\n\nfunc main() {\n    var speaker Speaker\n    person := Person{Name: \"John\"}\n    speaker = person  \/\/ \u9690\u5f0f\u8f6c\u6362\u4e3a\u63a5\u53e3\u7c7b\u578b\n\n    \/\/ \u4f7f\u7528\u7c7b\u578b\u65ad\u8a00\u5c06\u63a5\u53e3\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\n    p, ok := speaker.(Person)  \/\/ ok \u7528\u4e8e\u5224\u65ad\u8f6c\u6362\u662f\u5426\u6210\u529f\n    if ok {\n        fmt.Println(\"\u8f6c\u6362\u6210\u529f:\", p.Name)  \/\/ \u8f93\u51fa: \u8f6c\u6362\u6210\u529f: John\n    } else {\n        fmt.Println(\"\u8f6c\u6362\u5931\u8d25\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u7a7a\u63a5\u53e3\u4e0e\u7c7b\u578b\u65ad\u8a00<\/strong><\/h3>\n\n\n\n<p>Go \u8bed\u8a00\u4e2d\u7684\u7a7a\u63a5\u53e3 <code>interface{}<\/code> \u53ef\u4ee5\u5bb9\u7eb3\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\u3002\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u53ef\u4ee5\u5c06\u7a7a\u63a5\u53e3\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc printType(i interface{}) {\n    switch v := i.(type) {\n    case int:\n        fmt.Println(\"int \u7c7b\u578b:\", v)\n    case string:\n        fmt.Println(\"string \u7c7b\u578b:\", v)\n    default:\n        fmt.Println(\"\u672a\u77e5\u7c7b\u578b\")\n    }\n}\n\nfunc main() {\n    printType(42)        \/\/ \u8f93\u51fa: int \u7c7b\u578b: 42\n    printType(\"hello\")   \/\/ \u8f93\u51fa: string \u7c7b\u578b: hello\n    printType(3.14)      \/\/ \u8f93\u51fa: \u672a\u77e5\u7c7b\u578b\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u7c7b\u578b\u8f6c\u6362\u7684\u9650\u5236<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e0d\u652f\u6301\u9690\u5f0f\u8f6c\u6362<\/strong>\uff1aGo \u8bed\u8a00\u4e0d\u652f\u6301\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u5fc5\u987b\u4f7f\u7528\u663e\u5f0f\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u6784\u4f53\u548c\u63a5\u53e3\u4e4b\u95f4\u7684\u8f6c\u6362\u9700\u8981\u5b9e\u73b0\u63a5\u53e3<\/strong>\uff1a\u7ed3\u6784\u4f53\u53ea\u80fd\u8f6c\u6362\u4e3a\u5b9e\u73b0\u4e86\u63a5\u53e3\u7684\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>\u63a5\u53e3\u7684\u7c7b\u578b\u65ad\u8a00<\/strong>\uff1a\u63a5\u53e3\u7c7b\u578b\u5fc5\u987b\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u663e\u5f0f\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u663e\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a\u4f7f\u7528 <code>type(value)<\/code> \u8bed\u6cd5\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff0c\u9002\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\u3001\u5b57\u7b26\u4e32\u4e0e\u5176\u4ed6\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u6784\u4f53\u4e0e\u63a5\u53e3\u7684\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a\u7ed3\u6784\u4f53\u53ef\u4ee5\u9690\u5f0f\u8d4b\u503c\u7ed9\u63a5\u53e3\u7c7b\u578b\uff0c\u63a5\u53e3\u7c7b\u578b\u53ef\u4ee5\u901a\u8fc7\u7c7b\u578b\u65ad\u8a00\u8f6c\u6362\u4e3a\u5177\u4f53\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong>\u7a7a\u63a5\u53e3\u7684\u4f7f\u7528<\/strong>\uff1a\u7a7a\u63a5\u53e3 <code>interface{}<\/code> \u53ef\u4ee5\u5b58\u50a8\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u7ed3\u5408\u7c7b\u578b\u65ad\u8a00\u5b9e\u73b0\u4e0d\u540c\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\ud83d\udd39 \u53c2\u8003\u8d44\u6599<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/golang.org\/doc\/effective_go.html#types\">Go \u5b98\u65b9\u6587\u6863 &#8211; \u7c7b\u578b\u7cfb\u7edf<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/golang\/go\/wiki\/TypeConversions\">Go Wiki &#8211; \u7c7b\u578b\u8f6c\u6362<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Go \u8bed\u8a00\u662f\u4e00\u79cd\u5f3a\u7c7b\u578b\u8bed\u8a00\uff0c\u7c7b\u578b\u8f6c\u6362\u662f\u6307\u5c06\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u503c\u8f6c\u6362\u6210\u53e6\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u3002Go \u4e2d\u6709\u4e24\u79cd\u7c7b\u578b\u8f6c\u6362\uff1a\u663e\u5f0f\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3205,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58],"tags":[],"class_list":["post-3204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3204"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3204\/revisions"}],"predecessor-version":[{"id":3206,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3204\/revisions\/3206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3205"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}