Go (Golang) Read and Write File Example Tutorial

In this tutorial, we will learn how to read a file and how to write to file in Golang with examples.

To read files in Go, we use the os, ioutil, io, and bufio packages.

To write to files in Go, we use the os, ioutil, and fmt packages.

Table of contents

  • Go - read the file into a string
  • Go - read file line by line
  • Go - write to file with File.WriteString
  • Go - write to file with ioutil.WriteFile

Go - read the file into a string

The ioutil.ReafFile function reads the whole file into a string. This function is convenient but should not be used with very large files.

The example reads the whole file and prints it to the console.

Let's create a 'sample.txt' file and add the following content to it:

My first fav programming languge is Go Lang
My second fav programming languge is Java
My third fav programming languge is Python

Let's create a file named go_example.go and add the following content to it:


package main

import (
    "fmt"
    "io/ioutil"
    "log"
)

func main() {

    content, err := ioutil.ReadFile("sample.txt")

     if err != nil {
          log.Fatal(err)
     }

    fmt.Println(string(content))
}

Output:

G:\GoLang\examples>go run go_example.go
My first fav programming languge is Go Lang
My second fav programming languge is Java
My third fav programming languge is Python

Go - read file line by line

The Scanner provides a convenient interface for reading data such as a file of newline-delimited lines of text. It reads data by tokens; the Split function defines the token. By default, the function breaks the data into lines with line termination stripped.

The example reads the file line by line. Each line is printed.

Let's create a 'sample.txt' file and add the following content to it:

My first fav programming languge is Go Lang
My second fav programming languge is Java
My third fav programming languge is Python

Let's create a file named go_example.go and add the following content to it:


package main

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func main() {

    f, err := os.Open("thermopylae.txt")

    if err != nil {
        log.Fatal(err)
    }

    defer f.Close()

    scanner := bufio.NewScanner(f)

    for scanner.Scan() {

        fmt.Println(scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

Output:

G:\GoLang\examples>go run go_example.go
My first fav programming languge is Go Lang
My second fav programming languge is Java
My third fav programming languge is Python

Go - write to file with File.WriteString

The File.WriteString function writes the contents of a string to a file. The example writes a string to a text file.

Let's first create an empty 'sample.txt' file that we want to write upon.

Let's create a file named go_example.go and add the following content to it:


package main

import (
    "fmt"
    "log"
    "os"
)

func main() {

    f, err := os.Create("sample.txt")

    if err != nil {
        log.Fatal(err)
    }

    defer f.Close()

    _, err2 := f.WriteString("My first fav programming languge is Go Lang \nMy second fav programming languge is Java \nMy third fav programming languge is Python\n")

    if err2 != nil {
        log.Fatal(err2)
    }

    fmt.Println("done")
}

Output:

G:\GoLang\examples>go run go_example.go
done

After executing the above program, open the 'sample.txt' file and you should able to see the content of the file as below:

My first fav programming languge is Go Lang 
My second fav programming languge is Java 
My third fav programming languge is Python

Go - write to file with ioutil.WriteFile

The outil.WriteFile writes data to the specified file. This is a higher-level convenience function. The opening and closing of the file are handled for us.

The example writes a string to a file with ioutil.WriteFile.

Let's first create an empty 'sample.txt' file that we want to write upon.

Let's create a file named go_example.go and add the following content to it:


package main

import (
    "fmt"
    "io/ioutil"
    "log"
)

func main() {

    val := "My first fav programming languge is Go Lang \nMy second fav programming languge is Java \nMy third fav programming languge is Python\n"
    data := []byte(val)

    err := ioutil.WriteFile("data.txt", data, 0)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println("done")
}

Output:

G:\GoLang\examples>go run go_example.go
done

After executing the above program, open the 'sample.txt' file and you should able to see the content of the file as below:

My first fav programming languge is Go Lang 
My second fav programming languge is Java 
My third fav programming languge is Python

Golang Related Tutorials

Comments