Reading from file line by line is quite simple in golang, and can be done in few lines of code, let see how its done.

Here in this post, I will be using scanner from bufio package for it. First we will need package os for opening file from its location.

	fileHandle, _ := os.Open("file_name.txt")

You can pass relative(../file_name) or abosulte path(C:/folder_name/file_name) of file to Open method.

We will add a defer statement to close file, so that once our work is done file handle is closed, defer fileHandle.Close()

Now we need Scanner for reading line, we can create it using bufio package as

	fileScanner := bufio.NewScanner(fileHandle)

Now scan line by fileScanner.Scan() and get its content as string as fileScanner.Text()

We can use fileScanner.Scan() in for loop to determine end of file.

The complete code is as below, here I am reading line by line from file and printing it on console

Note:- I am using “_” for error, and not handling the error, but should be done in your production code, read about error handling here