使用 ioutil 讀取和寫入檔案
寫一個簡單的程式,寫著 Hello World!
到 test.txt
,讀取資料並列印出來。演示簡單的檔案 I / O 操作。
package main
import (
"fmt"
"io/ioutil"
)
func main() {
hello := []byte("Hello, world!")
// Write `Hello, world!` to test.txt that can read/written by user and read by others
err := ioutil.WriteFile("test.txt", hello, 0644)
if err != nil {
panic(err)
}
// Read test.txt
data, err := ioutil.ReadFile("test.txt")
if err != nil {
panic(err)
}
// Should output: `The file contains: Hello, world!`
fmt.Println("The file contains: " + string(data))
}