Golang Write String To File

[Solved] Golang Write String To File | Go - Code Explorer | yomemimo.com
Question : golang write string to file

Answered by : smjure

f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil { panic(err)
}
defer f.Close()
if _, err = f.WriteString(text); err != nil { panic(err)
}

Source : | Last Update : Wed, 24 Nov 21

Question : golang write file

Answered by : restu-wahyu-saputra

// write file
write1, _ := os.Create("index.txt")
write1.Write([]byte("hello wordl"))
ioutil.WriteFile("indexx.txt", []byte("hello wordl"), fs.ModePerm)
os.WriteFile("indexxx.txt", []byte("hello wordl"), fs.ModePerm)
write2 := strings.NewReader("hello wordl")
write2.WriteTo(os.Stdout)
write3 := bufio.NewReader(write2)
write3.WriteTo(os.Stdout)
write4 := bufio.NewWriter(os.Stdout)
write4.ReadFrom(write2)
// read file
read1, _ := fs.ReadFile(os.DirFS("."), "index.txt")
fmt.Println(string(read1))
read2, _ := os.ReadFile("index.txt")
fmt.Println(string(read2))
read3, _ := ioutil.ReadFile("index.txt")
fmt.Println(string(read3))

Source : | Last Update : Mon, 18 Apr 22

Answers related to golang write string to file

Code Explorer Popular Question For Go