{"id":57812,"date":"2024-10-10T21:21:49","date_gmt":"2024-10-10T13:21:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/57812\/"},"modified":"2024-10-10T21:21:49","modified_gmt":"2024-10-10T13:21:49","slug":"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%95%b8%e6%93%9a-i-o-%e5%b0%8d%e8%b1%a1%e5%8f%8a%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/57812\/","title":{"rendered":"GO\u6559\u7a0b: Go \u8a9e\u8a00\u6578\u64da I\/O \u5c0d\u8c61\u53ca\u64cd\u4f5c"},"content":{"rendered":"<h1 id=\"go%e6%95%99%e7%a8%8b-go-%e8%aa%9e%e8%a8%80%e6%95%b8%e6%93%9a-i-o-%e5%b0%8d%e8%b1%a1%e5%8f%8a%e6%93%8d%e4%bd%9c-ZpIFpbLCqA\">GO\u6559\u7a0b: Go \u8a9e\u8a00\u6578\u64da I\/O \u5c0d\u8c61\u53ca\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\u7de8\u7a0b\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\u9032\u884c\u6578\u64da\u8655\u7406\u6642\uff0cI\/O\uff08\u8f38\u5165\/\u8f38\u51fa\uff09\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Go \u8a9e\u8a00\u4e2d\u7684\u6578\u64da I\/O \u5c0d\u8c61\u53ca\u5176\u64cd\u4f5c\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728 Go \u4e2d\u9032\u884c\u6578\u64da\u8655\u7406\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e4%b8%ad%e7%9a%84-i-o-%e5%b0%8d%e8%b1%a1-ZpIFpbLCqA\">Go \u8a9e\u8a00\u4e2d\u7684 I\/O \u5c0d\u8c61<\/h2>\n<p>\u5728 Go \u8a9e\u8a00\u4e2d\uff0cI\/O \u64cd\u4f5c\u4e3b\u8981\u901a\u904e\u6a19\u6e96\u5eab\u4e2d\u7684 <code>io<\/code> \u548c <code>os<\/code> \u5305\u4f86\u5be6\u73fe\u3002\u9019\u4e9b\u5305\u63d0\u4f9b\u4e86\u591a\u7a2e I\/O \u5c0d\u8c61\u548c\u63a5\u53e3\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u65b9\u4fbf\u5730\u9032\u884c\u6587\u4ef6\u64cd\u4f5c\u3001\u7db2\u7d61\u901a\u4fe1\u7b49\u3002<\/p>\n<h3 id=\"1-%e5%9f%ba%e6%9c%ac-i-o-%e5%b0%8d%e8%b1%a1-ZpIFpbLCqA\">1. \u57fa\u672c I\/O \u5c0d\u8c61<\/h3>\n<ul>\n<li><code>os.File<\/code>: \u4ee3\u8868\u4e00\u500b\u6587\u4ef6\u6216\u5176\u4ed6 I\/O \u4f86\u6e90\u7684\u5c0d\u8c61\uff0c\u63d0\u4f9b\u4e86\u8b80\u53d6\u548c\u5beb\u5165\u6587\u4ef6\u7684\u529f\u80fd\u3002<\/li>\n<li><code>io.Reader<\/code>: \u4e00\u500b\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u8b80\u53d6\u6578\u64da\u7684\u529f\u80fd\u3002\u4efb\u4f55\u5be6\u73fe\u4e86\u9019\u500b\u63a5\u53e3\u7684\u5c0d\u8c61\u90fd\u53ef\u4ee5\u7528\u65bc\u8b80\u53d6\u6578\u64da\u3002<\/li>\n<li><code>io.Writer<\/code>: \u53e6\u4e00\u500b\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u5beb\u5165\u6578\u64da\u7684\u529f\u80fd\u3002\u4efb\u4f55\u5be6\u73fe\u4e86\u9019\u500b\u63a5\u53e3\u7684\u5c0d\u8c61\u90fd\u53ef\u4ee5\u7528\u65bc\u5beb\u5165\u6578\u64da\u3002<\/li>\n<\/ul>\n<h3 id=\"2-%e8%ae%80%e5%8f%96%e6%95%b8%e6%93%9a-ZpIFpbLCqA\">2. \u8b80\u53d6\u6578\u64da<\/h3>\n<p>\u5728 Go \u4e2d\uff0c\u8b80\u53d6\u6578\u64da\u901a\u5e38\u4f7f\u7528 <code>os.File<\/code> \u548c <code>io.Reader<\/code> \u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9e\u6587\u4ef6\u4e2d\u8b80\u53d6\u6578\u64da\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"io\/ioutil\"\n    \"log\"\n)\n\nfunc main() {\n    \/\/ \u6253\u958b\u6587\u4ef6\n    data, err := ioutil.ReadFile(\"example.txt\")\n    if err != nil {\n        log.Fatal(err)\n    }\n    \/\/ \u8f38\u51fa\u6587\u4ef6\u5167\u5bb9\n    fmt.Println(string(data))\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>ioutil.ReadFile<\/code> \u51fd\u6578\u4f86\u8b80\u53d6\u6587\u4ef6\u5167\u5bb9\uff0c\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\u9032\u884c\u8f38\u51fa\u3002<\/p>\n<h3 id=\"3-%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a-ZpIFpbLCqA\">3. \u5beb\u5165\u6578\u64da<\/h3>\n<p>\u5beb\u5165\u6578\u64da\u7684\u904e\u7a0b\u8207\u8b80\u53d6\u985e\u4f3c\uff0c\u4f7f\u7528 <code>os.File<\/code> \u548c <code>io.Writer<\/code> \u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u5beb\u5165\u6578\u64da\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>package main\n\nimport (\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    \/\/ \u5275\u5efa\u6216\u6253\u958b\u6587\u4ef6\n    file, err := os.Create(\"output.txt\")\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    defer file.Close()\n\n    \/\/ \u5beb\u5165\u6578\u64da\n    _, err = file.WriteString(\"Hello, Go I\/O!\")\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    fmt.Println(\"\u6578\u64da\u5df2\u5beb\u5165\u6587\u4ef6\")\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\u65b0\u6587\u4ef6\uff0c\u7136\u5f8c\u4f7f\u7528 <code>WriteString<\/code> \u65b9\u6cd5\u5c07\u5b57\u7b26\u4e32\u5beb\u5165\u6587\u4ef6\u3002<\/p>\n<h2 id=\"go-%e8%aa%9e%e8%a8%80%e4%b8%ad%e7%9a%84%e7%b7%a9%e8%a1%9d-i-o-ZpIFpbLCqA\">Go \u8a9e\u8a00\u4e2d\u7684\u7de9\u885d I\/O<\/h2>\n<p>\u70ba\u4e86\u63d0\u9ad8 I\/O \u64cd\u4f5c\u7684\u6548\u7387\uff0cGo \u63d0\u4f9b\u4e86\u7de9\u885d I\/O \u7684\u652f\u6301\u3002\u901a\u904e\u4f7f\u7528 <code>bufio<\/code> \u5305\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5275\u5efa\u7de9\u885d\u7684\u8b80\u53d6\u548c\u5beb\u5165\u5c0d\u8c61\uff0c\u5f9e\u800c\u6e1b\u5c11\u7cfb\u7d71\u8abf\u7528\u7684\u6b21\u6578\u3002<\/p>\n<h3 id=\"1-%e7%b7%a9%e8%a1%9d%e8%ae%80%e5%8f%96-ZpIFpbLCqA\">1. \u7de9\u885d\u8b80\u53d6<\/h3>\n<pre><code>package main\n\nimport (\n    \"bufio\"\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    file, err := os.Open(\"example.txt\")\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    defer file.Close()\n\n    scanner := bufio.NewScanner(file)\n    for scanner.Scan() {\n        fmt.Println(scanner.Text())\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>bufio.NewScanner<\/code> \u5275\u5efa\u4e00\u500b\u7de9\u885d\u8b80\u53d6\u5668\uff0c\u9010\u884c\u8b80\u53d6\u6587\u4ef6\u5167\u5bb9\u3002<\/p>\n<h3 id=\"2-%e7%b7%a9%e8%a1%9d%e5%af%ab%e5%85%a5-ZpIFpbLCqA\">2. \u7de9\u885d\u5beb\u5165<\/h3>\n<pre><code>package main\n\nimport (\n    \"bufio\"\n    \"fmt\"\n    \"os\"\n)\n\nfunc main() {\n    file, err := os.Create(\"output.txt\")\n    if err != nil {\n        fmt.Println(err)\n        return\n    }\n    defer file.Close()\n\n    writer := bufio.NewWriter(file)\n    writer.WriteString(\"Hello, buffered Go I\/O!\")\n    writer.Flush() \/\/ \u78ba\u4fdd\u6240\u6709\u6578\u64da\u90fd\u5beb\u5165\u6587\u4ef6\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>bufio.NewWriter<\/code> \u5275\u5efa\u4e00\u500b\u7de9\u885d\u5beb\u5165\u5668\uff0c\u4e26\u5728\u5beb\u5165\u5f8c\u8abf\u7528 <code>Flush<\/code> \u65b9\u6cd5\u4ee5\u78ba\u4fdd\u6240\u6709\u6578\u64da\u90fd\u88ab\u5beb\u5165\u6587\u4ef6\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ZpIFpbLCqA\">\u7e3d\u7d50<\/h2>\n<p>Go \u8a9e\u8a00\u63d0\u4f9b\u4e86\u5f37\u5927\u7684 I\/O \u64cd\u4f5c\u652f\u6301\uff0c\u901a\u904e <code>os<\/code> \u548c <code>io<\/code> \u5305\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u9032\u884c\u6587\u4ef6\u7684\u8b80\u53d6\u548c\u5beb\u5165\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u7de9\u885d I\/O \u7684\u4f7f\u7528\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u7121\u8ad6\u662f\u8655\u7406\u6587\u672c\u6587\u4ef6\u9084\u662f\u4e8c\u9032\u5236\u6578\u64da\uff0cGo \u90fd\u80fd\u63d0\u4f9b\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u6216\u5176\u4ed6\u76f8\u95dc\u670d\u52d9\u611f\u8208\u8da3\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Go \u8a9e\u8a00\u4e2d\u7684\u6578\u64da I\/O \u5c0d\u8c61\u53ca\u64cd\u4f5c\uff0c\u5b78\u7fd2\u5982\u4f55\u9ad8\u6548\u8655\u7406\u6578\u64da\u6d41\u8207\u6587\u4ef6\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-57812","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57812","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=57812"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57812\/revisions"}],"predecessor-version":[{"id":57813,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/57812\/revisions\/57813"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=57812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=57812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=57812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}