logo
Tutorials
Code examples
Topics
Courses
AndroidAngularBeginnersBlockchainBootstrapCakePHPCode examplesCodeIgniterCoursesCSSDev TipsDjangoElectronErrors solvedFlaskFlutterGitGoLangGraphQlJavaJavascriptJqueryKotlinLaravelMachine LearningMySQLNodePhalconPHPPreactPythonReactReact NativeSnippetsspring bootSQLSvelteSymfonyTutorialsUncategorizedVue
golang

How to read file line by line in Golang

Posted on: December 09, 2021 by Deven

In this article, you are going to learn about how to read file line by line in Golang.

Golang is an open-source programming language which is developed by Google. By using Golang you can do so many things, reading files line by line is one of them. To read a file in Golang, we will use bufio package scanner.

Let’s assume that, you have a test.txt file that contains some text. Here, In our case, we have a text.txt file that contains the following texts.

GO Language is an open-source statically compiled programming language. It was designed at Google by Rob Pike, Ken Thompson, and Robert Grieserner. It is also known as Golang. In this article we will learn about how to read line by line in Golang.

Now, we want to read this file line by line. To implement this in Golang follow the below code example:

package main
 
import (
        "bufio"
        "fmt"
        "log"
        "os"
)
func main() {
        readFile, err := os.Open("test.txt")
 
        if err != nil {
                log.Fatalf("failed to open file: %s", err)
        }
 
        fileScanner := bufio.NewScanner(readFile)
        fileScanner.Split(bufio.ScanLines)
        var fileTextLines []string
 
        for fileScanner.Scan() {
                fileTextLines = append(fileTextLines, fileScanner.Text())
        }
 
        readFile.Close()
 
        for _, eachline := range fileTextLines {
                fmt.Println(eachline)
        }
}

Here, at first, we import our required packages, and then we read our test.txt file with the help of os.Open(). We also handled errors if there’s any unwanted situation occurred and finally, we implemented our functionality for reading the file line by line with the help of bufio package.

Output:

If you run the file, you will see following output in your terminal

read file line by liread file line by line in Golang ne in Golang

You can see the exact same text that was consisted in our test.txt file. This is how you can read file line by line in Golang. Your directory name can be different from this.

Related Posts:

  • Getting started with Writing Test In Vue Applications
  • Setting Up Angular Authentication Using JWT
  • Learn How to read and write file in NodeJS
  • How to Model your data in MongoDB
  • Brief Overview Of Design Pattern Used in Laravel
  • Create a Sleek Note app With Flutter

categories :

Code examplesGoLang

Share on social media

//
PreviousNext
Deven

About the author

Deven
Deven is an Entrepreneur, and Full-stack developer, Constantly learning and experiencing new things. He currently runs CodeSource.io and Dunebook.com
See all posts

Useful front-end & UX tips, delivered once a week.

Useful front-end & UX tips, delivered once a week. 🎁

Dev Tips

How To Create NFT Ar

January 14, 2022

How To Create NFT Art With No Coding Experience​

Code Examples

How to trim whitespa

February 4, 2022

How to trim whitespace from a string in python

How to move files in

February 2, 2022

How to move files in PHP

How to copy a file i

February 2, 2022

How to copy a file in PHP using PHP’s copy () function

How to reindex an ar

February 2, 2022

How to reindex an array element in PHP

How to convert Integ

February 2, 2022

How to convert Integer to String in Python

Errors Solved

How to fix module no

February 4, 2022

How to fix module not found: can’t resolve in React

How to fix Vscode in

February 2, 2022

How to fix Vscode indentation

How to fix MongoErro

December 24, 2021

How to fix MongoError:e11000 duplicate key error collection

How to fix vue packa

December 24, 2021

How to fix vue packages version mismatch

How to fix typeerror

December 24, 2021

How to fix typeerror: $ is not a function in JavaScript

Contact
Guest writing
Copyright
Privacy
Dunebook
Learn React and React Native
AndroidAngularBeginnersBlockchainBootstrapCakePHPCode examplesCodeIgniterCoursesCSSDev TipsDjangoElectronErrors solvedFlaskFlutterGitGoLangGraphQlJavaJavascriptJqueryKotlinLaravelMachine LearningMySQLNodePhalconPHPPreactPythonReactReact NativeSnippetsspring bootSQLSvelteSymfonyTutorialsUncategorizedVue

© 2022 Codesource.io