2 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
需要使用具有寫入或追加權(quán)限的函數(shù):
package main
import "os"
func main() {
f, err := os.Create("file.txt")
if err != nil {
panic(err)
}
defer f.Close()
for range [10]struct{}{} {
f.WriteString("some text\n")
}
}
https://golang.org/pkg/os#Create

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
// Choose the permit you want with os.OpenFile flags
file, err := os.OpenFile(path, os.O_RDWR, 0644)
// or crate new file if not exist
file, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
// or append new data into your file with O_APPEND flag
file, err = os.OpenFile(path, os.O_APPEND, 0755)
文檔: https://pkg.go.dev/os#OpenFile
- 2 回答
- 0 關(guān)注
- 104 瀏覽
添加回答
舉報(bào)