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.
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,
Now we need Scanner for reading line, we can create it using bufio package as
Now scan line by
fileScanner.Scan() and get its content as string as
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