{"id":57814,"date":"2024-10-10T21:22:33","date_gmt":"2024-10-10T13:22:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57814\/"},"modified":"2024-10-10T21:22:33","modified_gmt":"2024-10-10T13:22:33","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%b4%94%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6%e7%9a%84%e8%ae%80%e5%af%ab%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57814\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u7d14\u6587\u672c\u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e7%b4%94%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6%e7%9a%84%e8%ae%80%e5%af%ab%e6%93%8d%e4%bd%9c-RFrwZTtgqA\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u7d14\u6587\u672c\u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c<\/h1>\n<p>Go \u8a9e\u8a00\uff08\u53c8\u7a31 Golang\uff09\u662f\u4e00\u7a2e\u975c\u614b\u985e\u578b\u3001\u7de8\u8b6f\u578b\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u56e0\u5176\u7c21\u6f54\u7684\u8a9e\u6cd5\u548c\u9ad8\u6548\u7684\u6027\u80fd\u800c\u53d7\u5230\u5ee3\u6cdb\u6b61\u8fce\u3002\u5728\u8a31\u591a\u61c9\u7528\u4e2d\uff0c\u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u9032\u884c\u7d14\u6587\u672c\u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e6%96%87%e4%bb%b6%e7%9a%84%e8%ae%80%e5%8f%96-RFrwZTtgqA\">\u6587\u4ef6\u7684\u8b80\u53d6<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0c\u8b80\u53d6\u6587\u4ef6\u4e3b\u8981\u4f7f\u7528\u6a19\u6e96\u5eab\u4e2d\u7684 <code>os<\/code> \u548c <code>bufio<\/code> \u5305\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u8b80\u53d6\u4e00\u500b\u7d14\u6587\u672c\u6587\u4ef6\u7684\u5167\u5bb9\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"bufio\"\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    \/\/ \u6253\u958b\u6587\u4ef6\n    file, err := os.Open(\"example.txt\")\n    if err != nil {\n        fmt.Println(\"Error opening file:\", err)\n        return\n    }\n    defer file.Close()\n\n    \/\/ \u4f7f\u7528 bufio \u8b80\u53d6\u6587\u4ef6\n    scanner := bufio.NewScanner(file)\n    for scanner.Scan() {\n        fmt.Println(scanner.Text())\n    }\n\n    \/\/ \u6aa2\u67e5\u662f\u5426\u6709\u932f\u8aa4\n    if err := scanner.Err(); err != nil {\n        fmt.Println(\"Error reading file:\", err)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u4f7f\u7528 <code>os.Open<\/code> \u51fd\u6578\u6253\u958b\u4e00\u500b\u540d\u70ba <code>example.txt<\/code> \u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u6253\u958b\u6210\u529f\uff0c\u6211\u5011\u4f7f\u7528 <code>bufio.NewScanner<\/code> \u5275\u5efa\u4e00\u500b\u6383\u63cf\u5668\uff0c\u7136\u5f8c\u9010\u884c\u8b80\u53d6\u6587\u4ef6\u5167\u5bb9\u4e26\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u6700\u5f8c\uff0c\u6211\u5011\u6aa2\u67e5\u662f\u5426\u5728\u8b80\u53d6\u904e\u7a0b\u4e2d\u767c\u751f\u4e86\u932f\u8aa4\u3002<\/p>\n<h2 id=\"%e6%96%87%e4%bb%b6%e7%9a%84%e5%af%ab%e5%85%a5-RFrwZTtgqA\">\u6587\u4ef6\u7684\u5beb\u5165<\/h2>\n<p>\u5beb\u5165\u6587\u4ef6\u7684\u904e\u7a0b\u8207\u8b80\u53d6\u76f8\u4f3c\uff0c\u4f46\u6211\u5011\u9700\u8981\u4f7f\u7528 <code>os.Create<\/code> \u51fd\u6578\u4f86\u5275\u5efa\u6216\u8986\u84cb\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5beb\u5165\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    \/\/ \u5275\u5efa\u6216\u8986\u84cb\u6587\u4ef6\n    file, err := os.Create(\"output.txt\")\n    if err != nil {\n        fmt.Println(\"Error creating file:\", err)\n        return\n    }\n    defer file.Close()\n\n    \/\/ \u5beb\u5165\u5167\u5bb9\n    _, err = file.WriteString(\"Hello, Go \u8a9e\u8a00\uff01n\u9019\u662f\u5beb\u5165\u6587\u4ef6\u7684\u793a\u4f8b\u3002\")\n    if err != nil {\n        fmt.Println(\"Error writing to file:\", err)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>os.Create<\/code> \u5275\u5efa\u4e00\u500b\u540d\u70ba <code>output.txt<\/code> \u7684\u6587\u4ef6\uff0c\u7136\u5f8c\u4f7f\u7528 <code>file.WriteString<\/code> \u65b9\u6cd5\u5c07\u5b57\u7b26\u4e32\u5beb\u5165\u6587\u4ef6\u3002\u5982\u679c\u5beb\u5165\u904e\u7a0b\u4e2d\u767c\u751f\u932f\u8aa4\uff0c\u6211\u5011\u6703\u6253\u5370\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e9%99%84%e5%8a%a0%e5%8a%9f%e8%83%bd%ef%bc%9a%e6%96%87%e4%bb%b6%e7%9a%84%e8%bf%bd%e5%8a%a0%e5%af%ab%e5%85%a5-RFrwZTtgqA\">\u9644\u52a0\u529f\u80fd\uff1a\u6587\u4ef6\u7684\u8ffd\u52a0\u5beb\u5165<\/h2>\n<p>\u5982\u679c\u6211\u5011\u5e0c\u671b\u5728\u4e0d\u8986\u84cb\u539f\u6709\u5167\u5bb9\u7684\u60c5\u6cc1\u4e0b\u5411\u6587\u4ef6\u8ffd\u52a0\u5167\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>os.OpenFile<\/code> \u51fd\u6578\uff0c\u4e26\u6307\u5b9a <code>os.O_APPEND<\/code> \u6a19\u8a8c\u3002\u4ee5\u4e0b\u662f\u8ffd\u52a0\u5beb\u5165\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    \/\/ \u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u958b\u6587\u4ef6\n    file, err := os.OpenFile(\"output.txt\", os.O_APPEND|os.O_WRONLY, 0644)\n    if err != nil {\n        fmt.Println(\"Error opening file:\", err)\n        return\n    }\n    defer file.Close()\n\n    \/\/ \u8ffd\u52a0\u5167\u5bb9\n    _, err = file.WriteString(\"n\u9019\u662f\u8ffd\u52a0\u7684\u5167\u5bb9\u3002\")\n    if err != nil {\n        fmt.Println(\"Error writing to file:\", err)\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>os.OpenFile<\/code> \u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u958b\u6587\u4ef6\uff0c\u7136\u5f8c\u5c07\u65b0\u5167\u5bb9\u5beb\u5165\u6587\u4ef6\u7684\u672b\u5c3e\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-RFrwZTtgqA\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u9032\u884c\u7d14\u6587\u672c\u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c\uff0c\u5305\u62ec\u6587\u4ef6\u7684\u8b80\u53d6\u3001\u5beb\u5165\u548c\u8ffd\u52a0\u5beb\u5165\u3002\u9019\u4e9b\u57fa\u672c\u64cd\u4f5c\u662f\u8a31\u591a\u61c9\u7528\u7a0b\u5e8f\u7684\u57fa\u790e\uff0c\u638c\u63e1\u5b83\u5011\u5c07\u6709\u52a9\u65bc\u958b\u767c\u66f4\u8907\u96dc\u7684\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 Go \u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f\u958b\u767c\u74b0\u5883\u9084\u662f\u751f\u7522\u74b0\u5883\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Go \u8a9e\u8a00\u4e2d\u9032\u884c\u7d14\u6587\u672c\u6587\u4ef6\u7684\u8b80\u5beb\u64cd\u4f5c\uff0c\u638c\u63e1\u57fa\u672c\u51fd\u6578\u8207\u5be6\u7528\u7bc4\u4f8b\uff0c\u63d0\u5347\u7de8\u7a0b\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-57814","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57814","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=57814"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57814\/revisions"}],"predecessor-version":[{"id":57815,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57814\/revisions\/57815"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}