Hello everyone, In this tutorial, you will learn how to write a text to file in Go language. The following example program has been tested and shared in the post.
Go – Writing Text to File
package main import ( "bufio" "fmt" "io/ioutil" "os" ) func main() { // reader to read text or content from user. reader := bufio.NewReader(os.Stdin) fmt.Println("Enter the text:") // reading the text or content from user text, _ := reader.ReadString('\n') // writing the content to file with content and permission err := ioutil.WriteFile("my_file_name.txt", []byte(text), 0755) // error handling if err != nil { fmt.Println("Unable to write content to file... %v", err) } }
Output
Enter the text: Hello everyone, my name is Dinesh Krishnan. I am a Software Developer and Technology Consultant

Reference
- https://golang.org/doc/
- https://golang.org/pkg/
- https://golang.org/pkg/fmt/
- https://golang.org/pkg/fmt/#Println
- https://golang.org/pkg/bufio/
- https://golang.org/pkg/bufio/#NewReader
- https://golang.org/pkg/bufio/#Reader.ReadString
- https://golang.org/pkg/os/
- https://golang.org/pkg/io/ioutil/#WriteFile
More from my site
Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.