3 ways to read file into variable in Golang [SOLVED]


GOLANG Solutions, GO

In this article, we are going to demonstrate and discuss how to read file and store content into a variable. We will cover following 3 methods which can be used to read a file and store it's content into a variable in go

  • //go:embed
  • ioutil.ReadFile()
  • os.Open()

 

Using variables in Golang

In Golang, Variable is used to store data for a particular data type. It is declared either using shorthand := or using var keywords. We have already covered golang variables in detail in our previous articles.

For example

package main

import "fmt"

func main() {

	// using shorthand :=
	fullName := "John Doe"
	fmt.Printf("Using Shorthand :=, the value is  %s \n", fullName)

	// using var keyword
	var company string
	company = "GoLinuxCloud"
	fmt.Printf("Using var keyword, the value is %s", company)
}

Output:-

$ go run main.go
Using Shorthand :=, the value is  John Doe 
Using var keyword, the value is GoLinuxCloud

In reading file content, through the use of Golang standard library such as os, ioutil, embed, fmt packages. which provide various functions capable to read file content. We shall focus on the following function in various packages:-

  • Using go:embed package which is applied inline with variable
  • Using ioutil.ReadFile() from io/ioutilpackage
  • using os.Open() from os package
NOTE:
We encourage you to read only a small file content into variables. But of its a larger file, kindly read it line by line or to the struct.

 

Setup Lab Environment

Using normal commands mkdir and touch in Linux to create a working directory and file as well as initialize using go mod init golinux_project, you may read more from our module article. check if the files are created using ls the command and print myFile.txt content using cat command.

$ mkdir golinux_project && cd golinux_project && touch main.go myFile.txt
$ go mod init golinux_project
$ ls
main.go
myFile.txt
go.mod
$ echo "Hello From GoLinuxCloud Academy" >> myFile.txt
$ cat myFile.txt
Hello From GoLinuxCloud Academy

We shall be using myFile.txt in this article.

 

Method 1:- Using go:embed function in Golang

In Golang, using the embed package allows us to access files embedded in the running Go program. We can use it by importing _ "embed" on the imports for a particular file. As the embed package is not directly used, you need to put _ in front of it so that the Go compiler won't complain. We can use the //go:embed directive to initialize a variable of type string, []byte, or FS with the contents of files read from the package directory or subdirectories at compile time.

You need to begin a line with //go:embed, which denotes a Go comment but is treated in a special way, followed by the path to the file you want to embed.

Using the file we created above i.e myFile.txt, using the embed package we can print its content at runtime

 

Example-1: We can embed one file into a string

package main

import (
	_ "embed"
	"fmt"
)

//go:embed myFile.txt
var str string

func main() {
	fmt.Println(str)
}

Output:-

$ go run main.go myFile.txt
Hello From GoLinuxCloud Academy

Explanation:-

Here, we just passed the filename inline //go:embed myFile.txt with the go:embed, the var str string variable declaration is used to store the content into str read from the file. we are able to print out file content in string format as shown in the output above.

 

Example-2: Using go:embed one file into a slice of bytes

package main

import (
	_ "embed"
	"fmt"
)

//go:embed myFile.txt
var byteData []byte

func main() {
	fmt.Println(string(byteData))
}

Output:-

$ go run main.go
[32 72 101 108 108 111 32 70 114 111 109 32 71 111 76 105 110 117 120 67 108 111 117 100 32 65 99 97 100 101 109 121 10]

Explanation:-

Here, we just passed the filename inline //go:embed myFile.txt with the go:embed, the var byteData []byte variable declaration is used to store the bytes read from the file. we are able to print out the bytes as shown in the output above.

 

Example-3: Using embed in reading more files into file system

package main

import (
	"embed"
	"fmt"
	"os"
)

//go:embed myFile.txt
var file embed.FS

func main() {
	fileName := os.Args[1]

	fileData, err := file.ReadFile(fileName)
	if err != nil {
		fmt.Println("could not read the file content")
	}
	print(string(fileData))
}

Output:-

$ go run main.go myFile.txt
Hello From GoLinuxCloud Academy

Explanation:-
Here, we just passed the filename inline //go:embed myFile.txt with the go:embed, the var file embed.FS variable declaration is used to store the content into file read from the file. we are able to print out file content in string format as shown in the output above. we use os.Args[] from os package to pass the filename into the function of fileData, err := file.ReadFile(fileName) which returns the fileData in bytes format and we can use the string() function to convert it into the human-readable format as shown above. This can be used for multiple files.

 

Method 2:- Using ioutil.ReadFile() function in Golang

Golang, provide us with io/ioutil package which contains numerous function such as ReadFile(), ReadAll(), etc. At this particular point, we shall use ioutil.ReadFile() which accepts filename input and presents the results in form of bytes. Below is an example.

package main

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

func ReadFile(fileName string) []byte {
	// get content from the file using ioutil package results will be in form of []bytes {10 30 40}
	fileContent, err := ioutil.ReadFile(fileName)
	if err != nil {
		fmt.Printf("Could not read the content in the file due to %v", err)
	}
	// results are in form of bytes [10 40 60]
	return fileContent
}

func main() {
	//  read file name from the terminal
	file := os.Args[1]

	fmt.Printf("Reading file content .... in the %s \n", file)

	// set the result into variable of data
	data := ReadFile(file)
	// print output by converting bytes into string
	fmt.Println(string(data))

}

Output:

$ go run main.go myFile.txt            
Reading file content .... in the you 
Hello From GoLinuxCloud Academy

Explanation:-

Here, we pass have created a func ReadFile(fileName string) []byte {} which accepts a fileName input of string data type and returns []bytes an array of bytes. main():- we use an os.Args[] from the os package, this function helps us to pass the filename on the terminal and its later mapped to the data := ReadFile(file) variable to the results from the defined function above. we convert the data result from bytes into a human-readable format using string() inbuilt functions.

 

Method 3 :- Using os.Open() function in Golang

In Golang, the Os package contains numerous functions which help us to read file content. Here we shall use os.Open() a function that enables us to read the file content into variables and later close the file to avoid memory leaks using defer file.close() the function. It's combined with ioutil.ReadAll() which reads the file content into a defined variable.

Below is an example.

package main

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

// ReadFileUsingOsPackage  using os package which accept filename and returns a string
func ReadFileUsingOsPackage(file string) string {
	// using os.Open() function to open file
	fileData, errOpeningFile := os.Open(file)
	if errOpeningFile != nil {
		fmt.Printf("failed to open file due to %s", errOpeningFile)
	}
	// close the file to ensure no memory leak
	defer fileData.Close()
	// get the content using ioutil.ReadAll() package returns data in form of bytes 40 50 ..
	data, errReadingFileData := ioutil.ReadAll(fileData)
	if errReadingFileData != nil {
		log.Fatalf("Sorry could not read file content due to %s", errReadingFileData)
	}
	// convert the content to string for humanreadable format i.e Hello
	return string(data)
}

func main() {
	// pass the file name on your terminal
	file := os.Args[1]
	// pass the file to ReadFileUsingOsPackage() function
	data := ReadFileUsingOsPackage(file)
	// print output
	fmt.Println(data)
}

Output:

$ go run main.go myFile.txt
Hello From GoLinuxCloud Academy

 

Summary

In this article, we have discussed various ways to read file content and store it into variables. Kindly note that only a small file content is encouraged to be stored into variables using the above methods. In case of large files kindly use the read line-by-line method provided in our articles. In Golang numerous ways to achieve this were using:- embed, os, ioutil packages with different supportive functions.

 

References

Variable scope
Go modules
File reading methods

 

Deepak Prasad

Deepak Prasad

Deepak Prasad is the founder of GoLinuxCloud, bringing over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, Networking, and Security. His extensive experience spans development, DevOps, networking, and security, ensuring robust and efficient solutions for diverse projects.

Certifications and Credentials:

  • Certified Kubernetes Application Developer (CKAD)
  • Go Developer Certification
  • Linux Foundation Certified System Administrator (LFCS)
  • Certified Ethical Hacker (CEH)
  • Python Institute PCAP (Certified Associate in Python Programming)
You can connect with him on his LinkedIn profile and join his Facebook and LinkedIn page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment