{"id":57674,"date":"2024-10-10T20:12:14","date_gmt":"2024-10-10T12:12:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57674\/"},"modified":"2024-10-10T20:12:14","modified_gmt":"2024-10-10T12:12:14","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%b8%b8%e7%94%a8%e5%85%a7%e7%bd%ae%e5%8c%85%e7%b0%a1%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57674\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u5e38\u7528\u5167\u7f6e\u5305\u7c21\u4ecb"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e5%b8%b8%e7%94%a8%e5%85%a7%e7%bd%ae%e5%8c%85%e7%b0%a1%e4%bb%8b-YfXfhhfpaD\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u5e38\u7528\u5167\u7f6e\u5305\u7c21\u4ecb<\/h1>\n<p>Go \u8a9e\u8a00\uff0c\u4f5c\u70ba\u4e00\u7a2e\u975c\u614b\u985e\u578b\u3001\u7de8\u8b6f\u578b\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u81ea2009\u5e74\u7531Google\u63a8\u51fa\u4ee5\u4f86\uff0c\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u9ad8\u6548\u7684\u6027\u80fd\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002Go \u8a9e\u8a00\u5167\u7f6e\u4e86\u591a\u500b\u529f\u80fd\u5f37\u5927\u7684\u5305\uff0c\u9019\u4e9b\u5305\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u8c50\u5bcc\u7684\u5de5\u5177\u548c\u51fd\u6578\uff0c\u80fd\u5920\u7c21\u5316\u958b\u767c\u904e\u7a0b\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u4e00\u4e9b\u5e38\u7528\u7684\u5167\u7f6e\u5305\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Go \u8a9e\u8a00\u3002<\/p>\n<h2 id=\"1-fmt-%e5%8c%85-YfXfhhfpaD\">1. fmt \u5305<\/h2>\n<p>fmt \u5305\u662fGo \u8a9e\u8a00\u4e2d\u6700\u5e38\u7528\u7684\u5305\u4e4b\u4e00\uff0c\u4e3b\u8981\u7528\u65bc\u683c\u5f0f\u5316\u8f38\u5165\u548c\u8f38\u51fa\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u7a2e\u51fd\u6578\u4f86\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3001\u6578\u5b57\u548c\u5176\u4ed6\u6578\u64da\u985e\u578b\u3002<\/p>\n<pre><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"Hello, World!\")\n    name := \"Go\"\n    fmt.Printf(\"Welcome to %s programming!n\", name)\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cfmt.Println \u7528\u65bc\u8f38\u51fa\u5b57\u7b26\u4e32\uff0c\u800c fmt.Printf \u5247\u53ef\u4ee5\u683c\u5f0f\u5316\u8f38\u51fa\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u63a7\u5236\u8f38\u51fa\u683c\u5f0f\u7684\u60c5\u6cc1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2 id=\"2-os-%e5%8c%85-YfXfhhfpaD\">2. os \u5305<\/h2>\n<p>os \u5305\u63d0\u4f9b\u4e86\u8207\u64cd\u4f5c\u7cfb\u7d71\u4ea4\u4e92\u7684\u529f\u80fd\uff0c\u5305\u62ec\u6587\u4ef6\u64cd\u4f5c\u3001\u74b0\u5883\u8b8a\u91cf\u7684\u8b80\u53d6\u7b49\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u9032\u884c\u7cfb\u7d71\u5c64\u7d1a\u7684\u64cd\u4f5c\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    \/\/ \u7372\u53d6\u74b0\u5883\u8b8a\u91cf\n    path := os.Getenv(\"PATH\")\n    fmt.Println(\"PATH:\", path)\n\n    \/\/ \u5275\u5efa\u4e00\u500b\u65b0\u6587\u4ef6\n    file, err := os.Create(\"example.txt\")\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    defer file.Close()\n    fmt.Println(\"File created successfully.\")\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 os.Getenv \u7372\u53d6\u74b0\u5883\u8b8a\u91cf\uff0c\u4e26\u4f7f\u7528 os.Create \u5275\u5efa\u4e00\u500b\u65b0\u6587\u4ef6\u3002<\/p>\n<h2 id=\"3-net-http-%e5%8c%85-YfXfhhfpaD\">3. net\/http \u5305<\/h2>\n<p>net\/http \u5305\u662fGo \u8a9e\u8a00\u4e2d\u7528\u65bc\u69cb\u5efaHTTP\u5ba2\u6236\u7aef\u548c\u670d\u52d9\u5668\u7684\u6838\u5fc3\u5305\u3002\u5b83\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684API\u4f86\u8655\u7406HTTP\u8acb\u6c42\u548c\u97ff\u61c9\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"net\/http\"\n)\n\nfunc handler(w http.ResponseWriter, r *http.Request) {\n    fmt.Fprintf(w, \"Hello, %s!\", r.URL.Path[1:])\n}\n\nfunc main() {\n    http.HandleFunc(\"\/\", handler)\n    http.ListenAndServe(\":8080\", nil)\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684HTTP\u670d\u52d9\u5668\uff0c\u7576\u7528\u6236\u8a2a\u554f\u6839\u8def\u5f91\u6642\uff0c\u670d\u52d9\u5668\u6703\u8fd4\u56de\u4e00\u500b\u6b61\u8fce\u6d88\u606f\u3002<\/p>\n<h2 id=\"4-time-%e5%8c%85-YfXfhhfpaD\">4. time \u5305<\/h2>\n<p>time \u5305\u63d0\u4f9b\u4e86\u6642\u9593\u548c\u65e5\u671f\u7684\u8655\u7406\u529f\u80fd\uff0c\u5305\u62ec\u8a08\u6642\u3001\u5ef6\u9072\u548c\u6642\u9593\u683c\u5f0f\u5316\u7b49\u3002\u9019\u5c0d\u65bc\u9700\u8981\u6642\u9593\u63a7\u5236\u7684\u61c9\u7528\u7a0b\u5e8f\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"time\"\n)\n\nfunc main() {\n    currentTime := time.Now()\n    fmt.Println(\"Current Time:\", currentTime)\n\n    \/\/ \u5ef6\u90722\u79d2\n    time.Sleep(2 * time.Second)\n    fmt.Println(\"2 seconds later...\")\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 time.Now() \u7372\u53d6\u7576\u524d\u6642\u9593\uff0c\u4e26\u4f7f\u7528 time.Sleep() \u9032\u884c\u5ef6\u9072\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"5-encoding-json-%e5%8c%85-YfXfhhfpaD\">5. encoding\/json \u5305<\/h2>\n<p>encoding\/json \u5305\u63d0\u4f9b\u4e86JSON\u7de8\u78bc\u548c\u89e3\u78bc\u7684\u529f\u80fd\uff0c\u9019\u5c0d\u65bc\u8655\u7406Web API\u548c\u6578\u64da\u4ea4\u63db\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Person struct {\n    Name string `json:\"name\"`\n    Age  int    `json:\"age\"`\n}\n\nfunc main() {\n    person := Person{Name: \"Alice\", Age: 30}\n    jsonData, _ := json.Marshal(person)\n    fmt.Println(string(jsonData))\n}\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u5c07\u4e00\u500b\u7d50\u69cb\u9ad4\u8f49\u63db\u70baJSON\u683c\u5f0f\uff0c\u9019\u5728\u8207\u524d\u7aef\u6216\u5176\u4ed6\u670d\u52d9\u9032\u884c\u6578\u64da\u4ea4\u4e92\u6642\u975e\u5e38\u5e38\u898b\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-YfXfhhfpaD\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u7684\u5167\u7f6e\u5305\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\uff0c\u5f9e\u57fa\u672c\u7684\u8f38\u5165\u8f38\u51fa\u5230\u7db2\u7d61\u8acb\u6c42\u548c\u6578\u64da\u8655\u7406\uff0c\u9019\u4e9b\u5305\u4f7f\u5f97\u958b\u767c\u904e\u7a0b\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002\u7121\u8ad6\u662f\u69cb\u5efaWeb\u61c9\u7528\u9084\u662f\u9032\u884c\u7cfb\u7d71\u7de8\u7a0b\uff0c\u638c\u63e1\u9019\u4e9b\u5167\u7f6e\u5305\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u7684\u5e38\u7528\u5167\u7f6e\u5305\uff0c\u4e86\u89e3\u5176\u529f\u80fd\u8207\u61c9\u7528\uff0c\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u6548\u7387\u8207\u6280\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-57674","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=57674"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57674\/revisions"}],"predecessor-version":[{"id":57675,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57674\/revisions\/57675"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}