How to Read a File Line by Line to String in Golang? Last Updated : 22 Jun, 2020 Comments Improve Suggest changes Like Article Like Report To read a file line by line the bufio package Scanner is used. Let the text file be named as sample.txt and the content inside the file is as follows: GO Language is a statically compiled programming language, It is an open-source language. It was designed at Google by Rob Pike, Ken Thompson, and Robert Grieserner. It is also known as Golang. Go language is a general-purpose programming language mainly meant for building large scale complex software. C package main import ( "bufio" "fmt" "log" "os" ) func main() { // os.Open() opens specific file in // read-only mode and this return // a pointer of type os. file, err := os.Open("sample.txt") if err != nil { log.Fatalf("failed to open") } // The bufio.NewScanner() function is called in which the // object os.File passed as its parameter and this returns a // object bufio.Scanner which is further used on the // bufio.Scanner.Split() method. scanner := bufio.NewScanner(file) // The bufio.ScanLines is used as an // input to the method bufio.Scanner.Split() // and then the scanning forwards to each // new line using the bufio.Scanner.Scan() // method. scanner.Split(bufio.ScanLines) var text []string for scanner.Scan() { text = append(text, scanner.Text()) } // The method os.File.Close() is called // on the os.File object to close the file file.Close() // and then a loop iterates through // and prints each of the slice values. for _, each_ln := range text { fmt.Println(each_ln) } } Output: Comment More infoAdvertise with us Next Article How to Read a File Line by Line to String in Golang? N nikki2398 Follow Improve Article Tags : Go Language Golang-File-Handling Similar Reads How to Read File Word By Word in Golang? File reading is such an important aspect of a programming language. We need to perform certain operations programmatically, using automation in file reading/writing allows us to create certain programs/projects which might have looked impossible otherwise. File InputTo work with files, we have to in 5 min read How To Read a File Line By Line Using Node.js? To read a file line by line in Node.js, there are several approaches that efficiently handle large files and minimize memory usage. In this article, we'll explore two popular approaches: using the Readline module (which is built into Node.js) and using the Line-reader module, a third-party package. 3 min read How to Read a CSV File in Golang? Golang offers a vast inbuilt library that can be used to perform read and write operations on files. To read a CSV file, the following methods are used in Golang: os.Open(): The os.Open() method opens the named file for reading. This method returns either the os.File pointer or an error. encoding/cs 4 min read How to Read and Write the Files in Golang? Golang offers a vast inbuilt library that can be used to perform read and write operations on files. In order to read from files on the local system, the io/ioutil module is put to use. The io/ioutil module is also used to write content to the file. This revised version reflects the changes made in 4 min read How to split a slice of bytes in Golang? In Golang, you can split a slice of bytes into multiple parts using the bytes.Split function. This is useful when dealing with data like encoded strings, file contents, or byte streams that must be divided by a specific delimiter.Examplepackage mainimport ( "bytes" "fmt")func main() { // Initial byt 3 min read Like