logo
Tutorials
Code examples
Courses
Dev Tips
Errors solved
Snippets
Technical writing program
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
  • Learn How to read and write file in NodeJS
  • Setting Up Angular Authentication Using JWT
  • Learn How to read input from STDIN in Golang
  • Brief Overview Of Design Pattern Used in Laravel
  • Building a blogging platform Using React, GraphQL, And…

categories :

Code examplesGoLang

tag :

golang

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​

Collection of the 20

October 13, 2021

Collection of the 20 useful Python Scripts

No Code Development

October 1, 2021

No Code Development As An Alternative For Programming

Top 4 Best Programmi

August 4, 2021

Top 4 Best Programming Languages for Beginner

How to prepare for a

July 16, 2021

How to prepare for a Data Science interview?

Code Examples

How to concatenate t

March 3, 2022

How to concatenate two lists in python

How to use math.log(

March 3, 2022

How to use math.log() function in Python

How to add a new ite

March 2, 2022

How to add a new item to a python dictionary

How to use switch st

March 2, 2022

How to use switch statement in React

How to use whereIn()

February 23, 2022

How to use whereIn() method in Laravel

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