A Guide to Working With Text Files in Go

As a programming language, Go has a wide range of inbuilt features, including the ability to create, read, and write files like text (.txt) files.

Files are important components of our daily lives in the modern world. Text files are useful for storing structured human-readable data without the use of external software or databases.

4

If you’re wondering how you could begin manipulating text files in Go, this article is the right place for you.

The bufio Package in Go

Thebufiopackage, which is a part of Go’s standard library, is used for buffered input and output operations, hence the name. Thebufiopackage builds upon theio.Readerandio.Writerinterfaces, which are extensively used in the implementation of the majority of Go’s standard packages.

Thebufiopackage goes one step further by implementing buffering, making it useful for IO operations involving textual data.

Article cover design for Handling text files in Go

To use thebufiopackage in your program, you should import it alongside other packages.

You probably heard when yougot started with Gothat it comes with everything you could possibly need as a programmer. For direct communication with your operating system, there is a package calledosthat provides platform-independent functions.

Output of program to read text from file

Theosandbufiopackages make text file manipulation incredibly simple.

Although the vast majority of theospackage’s functions are Unix-like, they all employ the Go style of error handling, which makes it simple to read error messages and debug code.

Output of program to read and write to text file

So, how do you manipulate files containing text in Go?

How to Read Data From a Text File

To read data from text files, you need to call theospackage’sOpen()function, which accepts the name of the file to be read as a parameter. Another method to get the file is by using theOpenFile()function.

The major difference betweenOpen()andOpenFile()is in their implementation.Open()opens a file in read-only mode (with theO_RDONLYflag), whileOpenFile()gives more flexibility by accepting three parameters namely:

golang write operation

Opens the file in read-only mode

Opens the file in write-only mode

Opens the file in read-write mode

Appends data to the file when writing

Creates a new file if none exists

Used with O_CREATE, to indicate that the file should only be created if it doesn’t exist

Opens file for synchronous I/O

Truncate the file on open, if possible

The code below shows how to read data from a text file in Go usingos.Open(), andScan()which in this case, runs in aGo for loop.

When you run the code above withgo run filename.go, it produces the output shown below:

If you pass in just the file name and not the path to theOpen()orOpenFile()function for read operations, Go assumes the file is in the present directory and returns an error if it doesn’t find it. You should pass in the path to the file if you need to work with files outside the current directory.

How to Write Data to a Text File

Thebufiopackage provides a bufferedWriterinterface that stores bytes of data and writes them to a file with the help of aFlush()function.

The next sample code is a continuation of the one to read a file. In this example, data is written to the existingtestfile.txtusing theO_WRONLYandO_APPENDflags.

Add the following lines of code to the program above, before the closing curly brace of themain()function:

The output of your complete program should look like this:

When you open thetestfile.txtfile, you will also see the new line appended at the end of the file just like the image below:

File Handling in Go Is Easy

Go allows you to work with a variety of files, not just text files. There are numerous additional inbuilt libraries and functions for working with different types of data from files, including JSON and CSV.

To see how they behave differently depending on the situation, it’s possible to experiment with the various flags and file permissions that are available for use withOpenFile().

URLs are the public interface to your app, so make sure you’re using them effectively with these powerful routing packages.

Make sure you don’t miss these movies and shows before Netflix removes them.

OneDrive is one of the best, but it has a catch.

Freeing up vital memory on Windows only takes a moment, and your computer will feel much faster once you’re done.

Some subscriptions are worth the recurring cost, but not these ones.

Lose your laptop without this feature, and you’ll wish you had turned it on.

Technology Explained

PC & Mobile