Go – How to Write a Text to File in Go Language

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 (

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)


Enter the text:

Hello everyone, my name is Dinesh Krishnan. I am a Software Developer and Technology Consultant

How to write a text to file in Go language


  1. https://golang.org/doc/
  2. https://golang.org/pkg/
  3. https://golang.org/pkg/fmt/
  4. https://golang.org/pkg/fmt/#Println
  5. https://golang.org/pkg/bufio/
  6. https://golang.org/pkg/bufio/#NewReader
  7. https://golang.org/pkg/bufio/#Reader.ReadString
  8. https://golang.org/pkg/os/
  9. https://golang.org/pkg/io/ioutil/#WriteFile