golang

Golang Scanner Package

Golang Scanner Package

In this lesson on Scanner package in Golang, we will study various examples on how to use Scanners in multiple ways in Go programming language. We will get started now.

Starting with Go

Just to make sure we have the environment setup consistently, here is the directory structure which I made for my Hello World program:

Here is the program we created:

package main
import "fmt"
func main()
fmt.Printf("Hello, world.\n")

We can run the above program with the following command:

go run hello.go

Once we run this command, here is the output you will see:

Now that looks good. Let's move to our main agenda.

Scanner and Bufio Package in Golang

In this post, we will go through the bufio and scanner packages.

We will start with a very simple example to split Strings into multiple words. Let's at the following example:

package main
import (
"bufio"
"fmt"
"strings"
)
func main()
inputStr := "golang shubham linux"
scanner := bufio.NewScanner(strings.NewReader(inputStr))
scanner.Split(bufio.ScanWords)
for scanner.Scan()
fmt.Println(scanner.Text())

The output of this program will be:

golang
shubham
linux

Here, Scanner used buffered input output by reading provided input as a Stream.

Reading a file

Now, let's try reading a file in Go, using bufio to read a file line by line. To do this, first we create a sample file in the same directory as our Go program. Here is our file:

Next, we write our program to read this file line by line:

package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main()
fileToRead, error := os.Open("./hello.txt")
if error != nil
log.Fatal(error)

defer fileToRead.Close()
scanner := bufio.NewScanner(fileToRead)
for scanner.Scan()
fmt.Println(scanner.Text())

if error := scanner.Err(); error != nil
log.Fatal(error)

Once we run this program, here is the output we will get

Taking User Input with bufio

This is the most useful operation actually to be performed when a user is starting with the Golang language.

We can take a user input like:

package main
import (
"bufio"
"fmt"
"os"
)
func main()
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
fmt.Println(text)

Let's run this program now:

Just to note, there is another way to take input if you are Ok NOT accepting a whitespace in it:

package main
import "fmt"
var input string
func main()
fmt.Print("Enter Your Name=")
fmt.Scanf("%s",&input)
fmt.Println("Hello "+input)

Let's run this program now:

Conclusion

To study, Scanner and Bufio package in Go is very useful and it is never possible to get enough. Read more examples for the package and try as much as possible on your own.

Mus Mus venstreklikknapp fungerer ikke på Windows 10
Mus venstreklikknapp fungerer ikke på Windows 10
Hvis du bruker en dedikert mus med den bærbare eller stasjonære datamaskinen, men museklikk-knappen fungerer ikke på Windows 10/8/7 av en eller annen ...
Mus Markøren hopper eller beveger seg tilfeldig mens du skriver inn Windows 10
Markøren hopper eller beveger seg tilfeldig mens du skriver inn Windows 10
Hvis du finner ut at musemarkøren hopper eller beveger seg alene, automatisk, tilfeldig mens du skriver inn Windows-bærbar PC eller datamaskin, kan no...
Mus Hvordan reversere rulle retning for mus og styreplater i Windows 10
Hvordan reversere rulle retning for mus og styreplater i Windows 10
Mus og Pekeplates gjør ikke bare databehandling enkelt, men mer effektivt og mindre tidkrevende. Vi kan ikke forestille oss et liv uten disse enhetene...