{"id":88,"date":"2024-02-07T21:58:37","date_gmt":"2024-02-07T13:58:37","guid":{"rendered":"https:\/\/669082.xyz\/?p=88"},"modified":"2024-02-08T08:01:32","modified_gmt":"2024-02-08T00:01:32","slug":"20240207%e5%ad%a6%e4%b9%a0golang_%e9%9d%9e%e7%b3%bb%e7%bb%9f%e5%8c%96%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/669082.xyz\/index.php\/2024\/02\/07\/20240207%e5%ad%a6%e4%b9%a0golang_%e9%9d%9e%e7%b3%bb%e7%bb%9f%e5%8c%96%e5%ad%a6%e4%b9%a0\/","title":{"rendered":"20240207\u5b66\u4e60golang_\u975e\u7cfb\u7edf\u5316\u5b66\u4e60"},"content":{"rendered":"<p>golang\u8f93\u51fahelloWorld<\/p>\n<pre><code class=\"language-go\">package main\nimport &quot;fmt&quot;\nfunc main() {\n    fmt.Println(&quot;hello world&quot;)\n}<\/code><\/pre>\n<h5>\u5173\u4e8e\u53d8\u91cf\u4e0e\u5e38\u91cf<\/h5>\n<p>golang\u53d8\u91cf\u58f0\u660e<\/p>\n<pre><code class=\"language-go\">a := &quot;abcdefg&quot;\nfmt.Printf(&quot;a = %s, type of a = %T\\n&quot;, a, a)\nfmt.Println(a)\n\nb := 1.23\nfmt.Printf(&quot;type of b = %T\\n&quot;, b)\nfmt.Println(b)<\/code><\/pre>\n<p>golang\u5e38\u91cf<\/p>\n<pre><code class=\"language-go\">const pi float64 = 3.1415\nfmt.Println(pi)<\/code><\/pre>\n<h5>\u5173\u4e8e\u51fd\u6570<\/h5>\n<p>golang\u51fd\u6570\u521d\u5b66<\/p>\n<pre><code class=\"language-go\">func main() {\n    s := fplus(2, 3)\n    fmt.Println(s)\n}\n\nfunc fplus(a int, b int)(c int){\n    c = a + b\n    return\n}<\/code><\/pre>\n<p>glonag\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u7ed3\u679c<\/p>\n<pre><code class=\"language-go\">func main() {\n    a, b := plusone(5, 6)\n    fmt.Println(a)\n    fmt.Println(b)\n}\n\nfunc plusone(a int, b int)(int, int){\n    return a + 1, b + 1\n}<\/code><\/pre>\n<h5>\u5bfc\u5305\u4e0einit\u51fd\u6570<\/h5>\n<p>\u56fe\u7247\u8bf4\u660e import\u5bfc\u5305\u4e0einit\u51fd\u6570<br \/>\n<img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/02\/image-1707319226359.png\" alt=\"file\" \/><br \/>\n\u8fd9\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u6587\u4ef6\u91cc\u6709\u5bfc\u5305\uff0c\u4f1a\u5148\u521d\u59cb\u5316\u5bfc\u5165\u7684\u5305\u91cc\u7684 \u5e38\u91cf\uff0c\u53d8\u91cf, \u6267\u884cinit\u51fd\u6570<br \/>\n\u6ce8\u610f:init\u51fd\u6570\u7684\u6267\u884c\u4f1a\u65e9\u4e8emain\u51fd\u6570<\/p>\n<p>\u5173\u4e8ego mod\u6ce8\u610f<br \/>\n\u5982\u679c\u5f00\u542f\u5e76\u4f7f\u7528\u4e86go mod \u5728\u5bfc\u5165\u81ea\u5df1\u5199\u7684\u5305\u7684\u65f6\u5019<br \/>\nimport module\u540d\/\u5305\u540d  \u8fd9\u4e2a\u5305\u540d\u5176\u5b9e\u662f\u662f\u6587\u4ef6\u5939\u540d \u540c\u65f6\u4e5f\u662f\u6587\u4ef6\u91cc\u58f0\u660e\u7684\u5305\u540d<\/p>\n<pre><code class=\"language-go\">package main\nimport (\n    &quot;fmt&quot;\n    \/\/study\u662fgo mod init\u7684\u65f6\u5019\u8f93\u5165\u7684module\u540d\n    \/\/lib1  lib2 \u5176\u5b9e\u662f\u6587\u4ef6\u5939\u540d  \u4e5f\u662f\u6587\u4ef6\u91cc\u7684package\u540d\n    &quot;study\/lib1&quot;\n    &quot;study\/lib2&quot;\n)\n\nfunc main() {\n    fmt.Println(&quot;abc&quot;)\n    \/\/\u51fd\u6570\u540d \u9996\u5b57\u6bcd\u5927\u5199\u624d\u80fd\u5916\u90e8\u8c03\u7528\n    lib1.Test()\n    lib2.Test()\n}<\/code><\/pre>\n<p>import\u533f\u540d\u5bfc\u5165 \u5176\u5b9e\u5c31\u662f\u7ed9\u5305\u8d77\u4e86\u4e2a\u522b\u540d\u3000_<\/p>\n<pre><code class=\"language-go\">package main\nimport (\n    &quot;fmt&quot;\n    \/\/\u5982\u679c\u53ea\u60f3\u8c03\u7528\u5305\u91cc\u7684init\u65b9\u6cd5 \u9700\u8981\u533f\u540d\u5bfc\u5165\n    _ &quot;study\/lib1&quot;\n    _ &quot;study\/lib2&quot;\n)\n\nfunc main() {\n    fmt.Println(&quot;abc&quot;)\n}<\/code><\/pre>\n<p>import\u5bfc\u5165\u7684\u5305\u4e5f\u53ef\u4ee5\u8d77\u4e2a\u522b\u540d<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n    &quot;fmt&quot;\n    \/\/\u8fd9\u91cc\u4e3a\u5bfc\u5165\u7684\u5305\u8d77\u4e2a\u522b\u540d\uff0c\u4e0b\u9762\u53ef\u4ee5\u7528\u522b\u540d\u6765\u8c03\u7528 \n    abc &quot;study\/lib1&quot;\n    def &quot;study\/lib2&quot;\n)\n\nfunc main() {\n\n    fmt.Println(&quot;abc&quot;)\n    abc.Test()\n    def.Test()\n}<\/code><\/pre>\n<h5>\u5173\u4e8e\u6307\u9488<\/h5>\n<p>golang\u6307\u9488\u521d\u5b66<\/p>\n<pre><code class=\"language-go\">func main() {\n    a := 10\n    \/\/&amp;a \u4ee3\u8868 \u53d6\u5730\u5740 \u5f15\u7528\n    changeValue(&amp;a)\n    fmt.Println(a)\n}\n\n\/\/*int \u4ee3\u8868\u6307\u9488\u7c7b\u578b \u5f15\u7528\u7c7b\u578b\nfunc changeValue(p *int){\n    \/\/*p \u4ee3\u8868\u53d6\u5185\u5b58\u5730\u5740\u91cc\u7684\u503c \u4e5f\u53eb\u89e3\u5f15\u7528\n    *p = 100\n}<\/code><\/pre>\n<h5>\u5173\u4e8edefer<\/h5>\n<p>defer\u5173\u5065\u5b57 \u50cf\u4e2a\u6790\u6784\u51fd\u6570<br \/>\n\u6ce8\u610f:defer\u6bd4return\u8fd8\u8981\u665a\u6267\u884c<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/defer\u5173\u5065\u5b57 \u6700\u540e\u6267\u884c\u7684\u8bed\u53e5 \u50cf\u662fPHP\u91cc\u7684\u6790\u6784\u51fd\u6570\n    defer fmt.Println(&quot;defer  ..&quot;)\n\n    fmt.Println(&quot;a&quot;)\n    fmt.Println(&quot;b&quot;)\n}<\/code><\/pre>\n<h5>\u5173\u4e8e\u6570\u7ec4\u4e0eslice<\/h5>\n<p>\u56fa\u5b9a\u957f\u5ea6\u6570\u7ec4\u4f7f\u7528(\u4e0d\u597d\u7528)<br \/>\n\u6ce8\u610f:\u4e0d\u5efa\u8bae\u4f7f\u7528 \u5efa\u8bae\u7528slice\u5207\u7247(\u52a8\u6001\u6570\u7ec4)<br \/>\n\u7531\u4e8e\u957f\u5ea6\u56fa\u5b9a,\u4f20\u7ed9\u51fd\u6570\u5f53\u53c2\u6570\u90fd\u4e0d\u597d\u4f20<br \/>\n\u6570\u7ec4\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u7684\u8bdd\u662f\u503c\u4f20\u9012<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u5b9a\u4e49\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\n    var myArray1 [10]int\n\n    \/\/\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4 \u540e\u97625\u4e2a\u5143\u7d20\u7531\u4e8e\u6ca1\u6709\u7ed9\u6570\u636e \u662f0\n    myArray2 := [10]int{1, 2, 3, 4, 5}\n\n    \/\/\u904d\u5386\n    for i := 0; i &lt; len(myArray1); i++ {\n        fmt.Println(myArray1[i])\n    }\n\n    \/\/\u53e6\u4e00\u79cd\u65b9\u6cd5\u904d\u5386\n    for index, value := range myArray2 {\n        fmt.Println(&quot;index=&quot;, index)\n        fmt.Println(&quot;value=&quot;, value)\n    }\n}<\/code><\/pre>\n<p>slice \u5207\u7247 \u52a8\u6001\u6570\u7ec4<br \/>\n\u6ce8\u610f \u5207\u7247\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u8bdd \u662f\u5730\u5740\u4f20\u9012 \u4e5f\u53eb\u5f15\u7528\u4f20\u9012<br \/>\n\u7528\u6cd5\u8ddf\u6570\u7ec4\u662f\u4e00\u6837\u7684,\u53ea\u662f\u9ad8\u7ea7\u4e00\u70b9<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u5b9a\u4e49\u4e00\u4e2a\u5207\u7247\n    mySlice := []int{1,2,3}\n    \/\/\u904d\u5386\n    for _, value := range mySlice{\n        fmt.Println(value)\n    }\n}<\/code><\/pre>\n<p>\u53e6\u4e00\u79cd\u5b9a\u4e49\u5207\u7247\u7684\u65b9\u5f0f<\/p>\n<pre><code class=\"language-go\">    \/\/\u58f0\u660e\u4e00\u4e2a\u5207\u7247 \u67093\u4e2a\u5bb9\u91cf\n    mySlice1 := make([]int, 3)\n    fmt.Println(mySlice1)   <\/code><\/pre>\n<p>\u5207\u7247\u7684\u957f\u5ea6\u548c\u5bb9\u91cf<\/p>\n<pre><code class=\"language-go\">    \/\/\u5207\u7247\u7684\u957f\u5ea6 \u548c \u5bb9\u91cf\n    numbers := make([]int, 3, 10)\n    fmt.Printf(&quot;len = %d, cap = %d\\n&quot;, len(numbers),  cap(numbers))<\/code><\/pre>\n<p>\u5411slice\u91cc\u7528append\u6dfb\u52a0\u5143\u7d20<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u5207\u7247\u7684\u957f\u5ea6 \u548c \u5bb9\u91cf \u5f53\u524d\u5bb9\u91cf\u4e3a5\n    numbers := make([]int, 3, 5)\n    \/\/\u5411slice\u91cc\u6dfb\u52a0\u5143\u7d20\n    numbers = append(numbers, 4)\n    numbers = append(numbers, 6)\n    \/\/\u5f53\u6dfb\u52a0\u7684\u5143\u7d20\u8d85\u8fc7\u5bb9\u91cf\u4e86,\u4f1a\u81ea\u52a8\u6269\u5bb9\n    numbers = append(numbers, 7)\n    fmt.Printf(&quot;len = %d, cap = %d\\n&quot;, len(numbers), cap(numbers))\n}<\/code><\/pre>\n<p>\u5207\u7247\u7684\u622a\u53d6 \u7528\u4e0b\u6807\u622a\u53d6<br \/>\n\u6ce8\u610f:\u622a\u53d6\u524d\u4e0e\u622a\u53d6\u540e\u7684\u662f\u540c\u4e00\u4e2a\u5207\u7247 \u6539\u53d8\u4efb\u4f55\u4e00\u4e2a \u53e6\u4e00\u4e2a\u4e5f\u4f1a\u53d8<br \/>\n\u5982\u679c\u4e0d\u60f3\u8fd9\u6837 \u53ef\u4ee5\u7528copy\u51fd\u6570 \u62f7\u8d1d\u4e00\u4efd\u518d\u622a\u53d6<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u5207\u7247\u7684\u622a\u53d6\n    mySlice := make([]int, 2)\n    mySlice[0] = 2\n    mySlice[1] = 3\n    mySlice = append(mySlice, 4)\n    \/\/\u622a\u53d6    s := arr[startIndex:endIndex]\n    newSlice := mySlice[1:2]\n    fmt.Println(newSlice)\n}<\/code><\/pre>\n<h5>\u5173\u4e8emap<\/h5>\n<p>\u6ce8\u610f:map\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u7684\u8bdd  \u662f \u5f15\u7528\u4f20\u9012<br \/>\nmap\u7b80\u5355\u4f7f\u7528<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/map\u7b2c\u4e00\u79cd\u58f0\u660e\u65b9\u5f0f\n    \/\/map\u58f0\u660e\n    var myMap1 map[string]string\n    \/\/make\u5bb9\u91cf\n    myMap1 = make(map[string]string, 3)\n    myMap1[&quot;one&quot;] = &quot;php&quot;\n    myMap1[&quot;two&quot;] = &quot;java&quot;\n    myMap1[&quot;three&quot;] = &quot;python&quot;\n    \/\/\u5bb9\u91cf\u7528\u5b8c\u4e86\u4f1a\u81ea\u52a8\u6269\u5bb9\n    myMap1[&quot;four&quot;] = &quot;rust&quot;\n\n    fmt.Println(myMap1)\n\n    \/\/map\u7b2c\u4e8c\u79cd\u58f0\u660e\n    test2 := make(map[string]string)\n    test2[&quot;one&quot;] = &quot;php&quot;\n    test2[&quot;two&quot;] = &quot;golang&quot;\n    test2[&quot;three&quot;] = &quot;java&quot;\n    fmt.Println(test2) \/\/map[one:php two:golang three:java]\n\n    \/\/map\u7b2c\u4e09\u79cd\u58f0\u660e\n    test3 := map[string]string{\n        &quot;one&quot; : &quot;php&quot;,\n        &quot;two&quot; : &quot;golang&quot;,\n        &quot;three&quot; : &quot;java&quot;,\n    }\n    fmt.Println(test3) \/\/map[one:php two:golang three:java]\n\n    \/\/\u590d\u6742\u4e00\u70b9\u7684map value\u53c8\u662f\u4e2amap\n    language := make(map[string]map[string]string)\n    language[&quot;php&quot;] = make(map[string]string, 2)\n    language[&quot;php&quot;][&quot;id&quot;] = &quot;1&quot;\n    language[&quot;php&quot;][&quot;desc&quot;] = &quot;php\u662f\u4e16\u754c\u4e0a\u6700\u7f8e\u7684\u8bed\u8a00&quot;\n    language[&quot;golang&quot;] = make(map[string]string, 2)\n    language[&quot;golang&quot;][&quot;id&quot;] = &quot;2&quot;\n    language[&quot;golang&quot;][&quot;desc&quot;] = &quot;golang\u6297\u5e76\u53d1\u975e\u5e38good&quot;\n\n    fmt.Println(language)\n\n}<\/code><\/pre>\n<p>map\u7684\u589e\u5220\u6539\u67e5<\/p>\n<pre><code class=\"language-go\">func main() {\n    language := make(map[string]map[string]string)\n    language[&quot;php&quot;] = make(map[string]string, 2)\n    language[&quot;php&quot;][&quot;id&quot;] = &quot;1&quot;\n    language[&quot;php&quot;][&quot;desc&quot;] = &quot;php\u662f\u4e16\u754c\u4e0a\u6700\u7f8e\u7684\u8bed\u8a00&quot;\n    language[&quot;golang&quot;] = make(map[string]string, 2)\n    language[&quot;golang&quot;][&quot;id&quot;] = &quot;2&quot;\n    language[&quot;golang&quot;][&quot;desc&quot;] = &quot;golang\u6297\u5e76\u53d1\u975e\u5e38good&quot;\n\n    \/\/fmt.Println(language) \/\/map[php:map[id:1 desc:php\u662f\u4e16\u754c\u4e0a\u6700\u7f8e\u7684\u8bed\u8a00] golang:map[id:2 desc:golang\u6297\u5e76\u53d1\u975e\u5e38good]]\n\n    \/\/\u589e\u5220\u6539\u67e5\n    val, key := language[&quot;php&quot;]  \/\/\u67e5\u627e\u662f\u5426\u6709php\u8fd9\u4e2a\u5b50\u5143\u7d20\n    if key {\n        fmt.Printf(&quot;%v\\n&quot;, val)\n    } else {\n        fmt.Printf(&quot;no\\n&quot;);\n    }\n\n    \/\/language[&quot;php&quot;][&quot;id&quot;] = &quot;3&quot; \/\/\u4fee\u6539\u4e86php\u5b50\u5143\u7d20\u7684id\u503c\n    \/\/language[&quot;php&quot;][&quot;nickname&quot;] = &quot;\u556a\u556a\u556a&quot; \/\/\u589e\u52a0php\u5143\u7d20\u91cc\u7684nickname\u503c\n    \/\/delete(language, &quot;php&quot;)  \/\/\u5220\u9664\u4e86php\u5b50\u5143\u7d20\n    fmt.Println(language)\n\n}<\/code><\/pre>\n<p>map\u589e\u5220\u6539\u67e5<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u58f0\u660e\u4e00\u4e2amap\n    city := make(map[string]string)\n\n    \/\/\u589e\u52a0\u5143\u7d20\n    city[&quot;usa&quot;] = &quot;newyork&quot;\n    city[&quot;japan&quot;] = &quot;tokay&quot;\n    city[&quot;uk&quot;] = &quot;london&quot;\n    \/\/fmt.Println(city)\n\n    \/\/\u5220\u9664 \u4f7f\u7528\u5065\u6765\u5220\u9664\n    delete(city, &quot;uk&quot;)\n    \/\/fmt.Println(city)\n\n    \/\/\u4fee\u6539\n    city[&quot;japan&quot;] = &quot;abc&quot;\n    \/\/fmt.Println(city)\n\n    \/\/\u67e5\u8be2 \u904d\u5386\n    for key, value :=  range city{\n        fmt.Println(&quot;key = &quot;, key)\n        fmt.Println(&quot;value = &quot;, value)\n    }\n}<\/code><\/pre>\n<h5>\u9762\u5411\u5bf9\u8c61<\/h5>\n<h5>struct \u7ed3\u6784\u4f53<\/h5>\n<p>struct\u76f8\u5f53\u4e8e\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u91cc\u7684\u7c7b<br \/>\n\u6ce8\u610f:\u7528struct\u6784\u9020\u51fa\u6765\u7684\u6570\u636e(new\u51fa\u6765\u7684\u5bf9\u8c61) \u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u7684\u8bdd\u662f\u503c\u4f20\u9012<br \/>\n\u6ce8\u610f:\u7c7b\u540d\u9996\u5b57\u6bcd\u5927\u5199\u8868\u793a\u5176\u5b83\u5305\u4e5f\u53ef\u4ee5\u8bbf\u95ee \u5c5e\u6027\u540d\u9996\u5b57\u6bcd\u5927\u5199\u8868\u793a\u53ef\u5bf9\u5916\u8bbf\u95ee \u65b9\u6cd5\u540d\u9996\u5b57\u6bcd\u5927\u5199\u8868\u793a\u5176\u5b83\u5305\u53ef\u8bbf\u95ee<br \/>\n\u7ed3\u6784\u4f53\u521d\u4f53\u9a8c<\/p>\n<pre><code class=\"language-go\">\/\/ \u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\ntype Book struct {\n    title string\n    auth  string\n}\n\nfunc main() {\n    \/\/\u7528\u7ed3\u6784\u4f53\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\n    var book1 Book\n    book1.title = &quot;\u597d\u597d\u5b66golang&quot;\n    book1.auth = &quot;\u5f20\u4e09&quot;\n    fmt.Println(book1)\n}<\/code><\/pre>\n<p>\u7ed9\u7ed3\u6784\u4f53\u52a0\u65b9\u6cd5<\/p>\n<pre><code class=\"language-go\">\/\/\u7ed3\u6784\u4f53\u5b9a\u4e49  \u76f8\u5f53\u4e8e\u7c7b\ntype Hero struct{\n    \/\/\u4e24\u4e2a\u5c5e\u6027\n    Name string\n    Age  int\n}\n\n\/\/\u7c7b\u7684\u65b9\u6cd5\u5b9a\u4e49\n\/\/\u8fd9\u91cc\u6700\u597d\u52a0\u4e2a*\u53f7 \u5426\u5219\u4f20\u8fdb\u6765\u7684\u5bf9\u8c61\u662f\u62f7\u8d1d\nfunc (this *Hero) Show(){\n    fmt.Println(&quot;name = &quot;, this.Name)\n    fmt.Println(&quot;age = &quot;, this.Age)\n}\n\n\/\/\u7c7b\u7684\u65b9\u6cd5\u5b9a\u4e49\n\/\/\u8fd9\u91cc\u6700\u597d\u52a0\u4e2a*\u53f7 \u5426\u5219\u4f20\u8fdb\u6765\u7684\u5bf9\u8c61\u662f\u62f7\u8d1d\nfunc (this *Hero) SetName(name string){\n    this.Name = name\n}\n\nfunc main() {\n    \/\/\u7528\u7ed3\u6784\u4f53\u6784\u9020\u4e00\u4e2a\u5bf9\u8c61\n    hero := Hero{Name: &quot;\u5f20\u4e09&quot;, Age: 20}\n    \/\/\u6539\u4e00\u4e0b\u540d\u5b57\n    hero.SetName(&quot;\u674e\u56db&quot;)\n    \/\/\u67e5\u770b\u4e00\u4e0b\u6539\u6210\u529f\u4e86\n    hero.Show()\n}<\/code><\/pre>\n<p>golang\u91cc\u9762\u7684\u7c7b\u7ee7\u627f(struct)<\/p>\n<pre><code class=\"language-go\">\/\/\u7236\u7c7b\ntype Human struct{\n    Name string\n}\n\n\/\/\u7236\u7c7b\u7684\u65b9\u6cd5\nfunc (this *Human) Eat(){\n    fmt.Println(&quot;\u7236\u7c7b\u7684eat\u65b9\u6cd5&quot;)\n}\n\n\/\/\u7236\u7c7b\u7684\u65b9\u6cd5\nfunc (this *Human) Walk(){\n    fmt.Println(&quot;\u7236\u7c7b\u7684Walk\u65b9\u6cd5&quot;)\n}\n\n\/\/====================================\n\/\/\u5b50\u7c7b\ntype Superman struct{\n    \/\/\u7ee7\u627f\u7236\u7c7b,\u76f4\u63a5\u628a\u7236\u7c7b\u5199\u5728\u8fd9\u91cc\u5c31\u884c\n    Human\n\n    \/\/\u518d\u5199\u4e2a\u5b50\u7c7b\u7684\u5c5e\u6027\n    Age string\n}\n\n\/\/\u5b50\u7c7b\u7684\u65b9\u6cd5\nfunc (this *Superman) Run(){\n    fmt.Println(&quot;\u5b50\u7c7b\u7684Run\u65b9\u6cd5&quot;)\n}\n\n\/\/\u4e5f\u53ef\u4ee5\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\nfunc (this *Superman) Walk(){\n    fmt.Println(&quot;\u5b50\u7c7b\u91cd\u5199\u4e86\u7236\u7c7b\u7684Walk\u65b9\u6cd5&quot;)\n}\n\nfunc main() {\n    \/\/\u7528\u5b50\u7c7b\u6765new\u4e00\u4e2a\u5bf9\u8c61\n    var superman1 Superman\n    superman1.Name = &quot;\u5f20\u4e09&quot;\n    superman1.Age = &quot;100&quot;\n\n    \/\/\u8c03\u7528\u4e00\u4e0b\u7236\u7c7b\u7684\u65b9\u6cd5\n    superman1.Eat()\n\n    \/\/\u8c03\u7528\u4e00\u4e0b\u5b50\u7c7b\u7684\u65b9\u6cd5\n    superman1.Run()\n\n    \/\/\u8c03\u7528\u4e00\u4e0b \u5b50\u7c7b\u91cd\u5199\u7684\u7236\u7c7b\u65b9\u6cd5\n    superman1.Walk()\n\n    \/\/\u518d\u8c03\u7528\u4e00\u4e0b\u5c5e\u6027\n    fmt.Println(superman1.Name)\n    fmt.Println(superman1.Age)\n\n}<\/code><\/pre>\n<p>interface \u7a7a\u6307\u9488 \u4e07\u80fd\u7c7b\u578b \u7c7b\u578b\u65ad\u8a00<br \/>\n\u7a7a\u6307\u9488\u53ef\u4ee5\u63a5\u6536\u4efb\u52a1\u7c7b\u578b\u7684\u53c2\u6570<br \/>\n\u7c7b\u578b\u65ad\u8a00\u80fd\u591f\u5224\u65ad\u4f20\u9012\u8fc7\u6765\u6570\u636e\u7684\u7c7b\u578b<\/p>\n<pre><code class=\"language-go\">func myfunc(arg interface{}){\n    \/\/\u7c7b\u578b\u65ad\u8a00\n    _, ok := arg.(string)\n    if ok {\n        fmt.Println(&quot;\u662fstring\u7c7b\u578b&quot;)\n    }\n\n    _, ok = arg.(int)\n    if ok {\n        fmt.Println(&quot;\u662fint\u7c7b\u578b&quot;)\n    }\n}\n\nfunc main() {\n    myfunc(11)\n}<\/code><\/pre>\n<h5>reflect\u53cd\u5c04<\/h5>\n<p>\u53cd\u5c04\u83b7\u53d6\u53d8\u91cf\u7684\u7c7b\u578b\u548c\u503c<\/p>\n<pre><code class=\"language-go\">func myReflect(arg interface{}){\n    fmt.Println(reflect.TypeOf(arg))\n    fmt.Println(reflect.ValueOf(arg))\n} \n\nfunc main() {\n    myReflect(11)\n}\n<\/code><\/pre>\n<p>\u5229\u7528reflect\u83b7\u53d6\u7c7b\u7684\u6807\u7b7e<\/p>\n<pre><code class=\"language-go\">\/\/ \u5e26\u6807\u7b7e\u7684\u7ed3\u6784\u4f53\ntype Book struct {\n    Name string `info:&quot;\u4e66\u540d&quot;`\n    auth string `info:&quot;\u4f5c\u8005&quot;`\n}\n\n\/\/ \u5199\u4e2a\u51fd\u6570\u6765\u67e5\u8be2\u5bf9\u8c61\u7684\u6807\u7b7e\nfunc findTag(str interface{}) {\n    \/\/\u83b7\u53d6\u5bf9\u8c61\u7684\u5143\u7d20\n    t := reflect.TypeOf(str).Elem()\n    for i := 0; i &lt; t.NumField(); i++ {\n        infoTag := t.Field(i).Tag.Get(&quot;info&quot;)\n        fmt.Println(infoTag)\n    }\n\n}\n\nfunc main() {\n    book1 := Book{}\n    findTag(&amp;book1)\n}<\/code><\/pre>\n<h5>json<\/h5>\n<p>\u7ed3\u6784\u4f53\u4e0ejson\u4e92\u8f6c \u4f1a\u7528\u5230\u7ed3\u6784\u4f53\u6807\u7b7e<\/p>\n<pre><code class=\"language-go\">import (\n    &quot;encoding\/json&quot;\n    &quot;fmt&quot;\n)\n\n\/\/ \u5b9a\u4e49\u4e2a\u7ed3\u6784\u4f53\ntype Movie struct {\n    Name  string `json:&quot;name&quot;`\n    Year  int    `json:&quot;year&quot;`\n    Price int    `json:&quot;price&quot;`\n}\n\nfunc main() {\n    \/\/\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u4f53\n    movie1 := Movie{&quot;\u529f\u592b&quot;, 2005, 15}\n    \/\/\u7ed3\u6784\u4f53\u8f6cjson\n    jsonStr, err := json.Marshal(movie1)\n    if err != nil {\n        fmt.Println(&quot;\u8f6c\u6362json\u5931\u8d25&quot;)\n    }\n\n    fmt.Printf(&quot;\u8f6c\u6362json\u6210\u529f str=%s\\n&quot;, jsonStr)\n\n    \/\/\u518d\u8f6c\u56de\u6765\n    movie2 := Movie{}\n    err = json.Unmarshal(jsonStr, &amp;movie2)\n    if err != nil {\n        fmt.Println(&quot;\u8f6c\u6362\u5931\u8d25&quot;)\n    }\n    fmt.Println(movie2)\n}<\/code><\/pre>\n<h5>goroutine  \u534f\u7a0b  \u5e76\u53d1<\/h5>\n<p>\u534f\u7a0b\u521d\u4f53\u9a8c<\/p>\n<pre><code class=\"language-go\">import (\n    &quot;fmt&quot;\n    &quot;time&quot;\n)\n\nfunc task() {\n    i := 0\n    for {\n        fmt.Println(&quot;\u534f\u7a0b\u91cc\u6267\u884c\u7684=&quot;, i)\n        i++\n        time.Sleep(1 * time.Second)\n    }\n}\n\nfunc main() {\n    \/\/\u5f00\u4e00\u4e2ago\u7a0b\n    go task()\n\n    \/\/\u4e3b\u7ebf\u7a0b\n    i := 0\n    for{\n        fmt.Println(&quot;\u4e3b\u7ebf\u7a0b\u91cc\u6267\u884c\u7684=&quot;, i)\n        i ++\n        time.Sleep(1 * time.Second)\n    }\n}<\/code><\/pre>\n<h5>channel \u7ba1\u9053 \u7528\u4e8e\u534f\u7a0b\u95f4\u901a\u8baf<\/h5>\n<p>\u6ce8\u610f:\u7ba1\u7406\u5206\u4e3a\u6709\u7f13\u5b58\u548c\u6ca1\u6709\u7f13\u5b58\u4e24\u79cd<br \/>\n\u6ca1\u6709\u7f13\u5b58\u7684\u7ba1\u9053\u4f1a\u6709\u963b\u585e\u6548\u679c,\u4e0d\u7ba1\u662f\u5bf9\u4e8e\u63a5\u6536\u8fd8\u662f\u53d1\u9001<br \/>\n\u7ba1\u9053\u521d\u4f53\u9a8c<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u5b9a\u4e49\u4e00\u4e2achannel\n    c := make(chan int)\n\n    go func()  {\n        fmt.Println(&quot;\u5b50\u534f\u7a0b\u5f00\u59cb\u8fd0\u884c...&quot;)\n        defer fmt.Println(&quot;\u5b50\u534f\u7a0b\u8fd0\u884c\u7ed3\u675f&quot;)\n\n        \/\/\u5b50\u534f\u7a0b\u7ed3\u675f\u540e\u53d1\u9001\u4e2a\u4fe1\u53f7\n        c &lt;- 1\n\n    }()\n\n    \/\/\u4e3b\u534f\u7a0b\u7b49\u5f85\u63a5\u6536\u4fe1\u53f7\n    num := &lt;- c\n    fmt.Println(num)\n    fmt.Println(&quot;\u4e3b\u534f\u7a0b\u7ed3\u675f....&quot;)\n}<\/code><\/pre>\n<p>\u56fe\u89e3\u65e0\u7f13\u5b58channel<br \/>\n<img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/02\/image-1707342326035.png\" alt=\"file\" \/><br \/>\n\u56fe\u89e3\u6709\u7f13\u5b58channel<br \/>\n<img decoding=\"async\" src=\"https:\/\/669082.xyz\/wp-content\/uploads\/2024\/02\/image-1707342374052.png\" alt=\"file\" \/><\/p>\n<p>\u5e26\u7f13\u5b58\u7684channel\u7b80\u5355\u5e94\u7528<br \/>\n\u5e26\u7f13\u5b58\u7684channel\u5f53\u7f13\u5b58\u6ee1\u4e86 \u4e5f\u4f1a\u6709\u963b\u585e\u6548\u679c<\/p>\n<pre><code class=\"language-go\">import (\n    &quot;fmt&quot;\n    &quot;time&quot;\n)\n\nfunc main() {\n    \/\/\u5e26\u7f13\u5b58\u7684channel \u5bb9\u91cfcap\u4e3a3  \u957f\u5ea6len\u4e3a0\n    c := make(chan int, 3)\n    \/\/fmt.Printf(&quot;cap = %d, len = %d&quot;, cap(c), len(c))\n\n    go func()  {\n        defer fmt.Println(&quot;\u5b50go\u7a0b\u7ed3\u675f&quot;)\n        for i := 0; i &lt; 30; i++ {\n            \/\/\u5411\u7ba1\u9053\u4e2d\u5199\u5165\u6570\u636e\n            c &lt;- i\n            fmt.Println(&quot;\u5b50go\u7a0b\u5411\u7ba1\u9053\u4e2d\u5199\u5165\u4e86\u6570\u636e&quot;, i)\n        }\n    }()\n\n    time.Sleep(1 * time.Second)\n\n    for i := 0; i &lt; 30; i++ {\n        num := &lt;- c\n        fmt.Println(&quot;\u4e3bgo\u7a0b\u4ece\u7ba1\u9053\u4e2d\u8bfb\u53d6\u4e86\u6570\u636e&quot;, num)\n    }\n\n    fmt.Println(&quot;\u4e3bgo\u7a0b\u7ed3\u675f&quot;)\n}\n<\/code><\/pre>\n<p>\u7528close\u5173\u95ed\u7ba1\u9053\u7684\u7b80\u5355\u5e94\u7528<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u6ca1\u6709\u7f13\u5b58\u7684channel\n    c := make(chan int)\n\n    go func () {\n        for i := 0; i &lt; 5; i++ {\n            \/\/\u5411\u7ba1\u9053\u5199\u5165\u6570\u636e\n            c &lt;- i\n        }\n\n        \/\/\u5173\u95ed\u7ba1\u9053\n        close(c)\n    }()\n\n    for{\n        \/\/\u4e5f\u53ef\u4ee5\u8fd9\u6837\u4ece\u7ba1\u9053\u4e2d\u8bfb\u53d6\u6570\u636e\n        data, ok := &lt;- c\n        if ok {\n            fmt.Println(&quot;\u4ece\u7ba1\u9053\u4e2d\u8bfb\u53d6\u7684\u6570\u636e\u662f&quot;, data)\n        } else {\n            fmt.Println(&quot;\u7ba1\u9053\u5173\u95ed&quot;)\n            break\n        }\n    }\n\n    fmt.Println(&quot;\u4e3bgo\u7a0b\u7ed3\u675f&quot;)\n}<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u7528range\u6765 \u904d\u5386 \u4e0d\u65ad\u64cd\u4f5c\u901a\u9053<\/p>\n<pre><code class=\"language-go\">func main() {\n    \/\/\u6ca1\u6709\u7f13\u5b58\u7684channel\n    c := make(chan int)\n\n    go func () {\n        for i := 0; i &lt; 5; i++ {\n            \/\/\u5411\u7ba1\u9053\u5199\u5165\u6570\u636e\n            c &lt;- i\n        }\n\n        \/\/\u5173\u95ed\u7ba1\u9053\n        close(c)\n    }()\n\n    \/\/\u4e5f\u53ef\u4ee5\u7528range\u6765 \u4e0d\u65ad\u64cd\u4f5cchannel\n    for data := range c{\n        fmt.Println(data)\n    }\n\n    fmt.Println(&quot;\u4e3bgo\u7a0b\u7ed3\u675f&quot;)\n}<\/code><\/pre>\n<h5>select\u64cd\u4f5c\u901a\u9053<\/h5>\n<p>demo<\/p>\n<pre><code class=\"language-go\">    select {\n    case &lt;- chan1:\n        \/\/ \u5982\u679cchan1\u6210\u529f\u8bfb\u5230\u6570\u636e\uff0c\u5219\u8fdb\u884c\u8be5case\u5904\u7406\u8bed\u53e5\n    case chan2 &lt;- 1:\n        \/\/ \u5982\u679c\u6210\u529f\u5411chan2\u5199\u5165\u6570\u636e\uff0c\u5219\u8fdb\u884c\u8be5case\u5904\u7406\u8bed\u53e5\n    default:\n        \/\/ \u5982\u679c\u4e0a\u9762\u90fd\u6ca1\u6709\u6210\u529f\uff0c\u5219\u8fdb\u5165default\u5904\u7406\u6d41\u7a0b\n    }<\/code><\/pre>\n<p>select\u53ef\u4ee5\u540c\u65f6\u76d1\u63a7\u591a\u8defchannel\u7684\u72b6\u6001<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n    &quot;fmt&quot;\n)\n\nfunc fibonacci(c, quit chan int) {\n    x, y := 1, 1\n    for {\n        select {\n        case c &lt;- x:\n            x, y = y, x+y\n        case &lt;-quit:\n            fmt.Println(&quot;quit&quot;)\n            return\n        }\n    }\n}\n\nfunc main() {\n    c := make(chan int)\n    quit := make(chan int)\n\n    go func() {\n        for i := 0; i &lt; 6; i++ {\n            fmt.Println(&lt;-c)\n        }\n        quit &lt;- 0\n    }()\n\n    fibonacci(c, quit)\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>golang\u8f93\u51fahelloWorld package main import &quot;fmt&#038;q [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":59,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/posts\/88\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/669082.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}