一个写“你好,世界!”的简单程序。至test.txt,读回数据并打印出来。演示简单的文件I / O操作。
package main import ( "fmt" "io/ioutil" ) func main() { hello := []byte("Hello, world!") // 编写“ Hello,world!” totest.txt,该文件可以由用户读取/写入并由其他人读取 err := ioutil.WriteFile("test.txt", hello, 0644) if err != nil { panic(err) } // 阅读test.txt data, err := ioutil.ReadFile("test.txt") if err != nil { panic(err) } // 应该输出:`文件包含:Hello,world!` fmt.Println("该文件包含: " + string(data)) }