DEV Community

Cover image for How to Scan in GoLang
Steve Yonkeu
Steve Yonkeu

Posted on

How to Scan in GoLang

In Go (golang), the fmt package provides several functions for scanning input from the console or other input sources.

For me these have always been useful during test and so many other areas. And so far I usually work with 4 functions during scanning.

Let's explore some of them and see how, why and when to use it.


1. fmt.Scan

  • Purpose: Reads space-separated input from standard input (console).
  • Stops Reading: Stops on the first newline or whitespace.
  • Multiple Variables: Can read multiple variables in one call.
  • Returns: Number of successfully scanned items and error if any.

Example:

package main

import (
    "fmt"
)

func main() {
    var name string
    var age int
    fmt.Print("Enter your name and age: ")
    fmt.Scan(&name, &age) // Reading input separated by space
    fmt.Printf("Hello %s, you are %d years old.\n", name, age)
}
Enter fullscreen mode Exit fullscreen mode

Input Example:

Alice 25

Output:

Hello Alice, you are 25 years old.
Enter fullscreen mode Exit fullscreen mode

2. fmt.Scanln

  • Purpose: Reads input until a newline (\n) is encountered.
  • Stops Reading: Stops on newline instead of whitespace.
  • Multiple Variables: Can read multiple variables but stops if it reaches newline first.
  • Returns: Number of successfully scanned items and error if any.

Example:

package main

import (
    "fmt"
)

func main() {
    var name string
    var age int
    fmt.Print("Enter your name and age: ")
    fmt.Scanln(&name, &age) // Reads until newline is encountered
    fmt.Printf("Hello %s, you are %d years old.\n", name, age)
}
Enter fullscreen mode Exit fullscreen mode

Input Example:

Alice 25

Output:

Hello Alice, you are 25 years old.
Enter fullscreen mode Exit fullscreen mode

3. fmt.Scanf

  • Purpose: Reads formatted input using format specifiers (like %s, %d, %f).
  • Stops Reading: Stops based on the specified format.
  • Multiple Variables: Can read multiple variables with precise control using format specifiers.
  • Returns: Number of successfully scanned items and error if any.

Example:

package main

import (
    "fmt"
)

func main() {
    var name string
    var age int
    fmt.Print("Enter your name and age (formatted): ")
    fmt.Scanf("%s %d", &name, &age) // Reads formatted input
    fmt.Printf("Hello %s, you are %d years old.\n", name, age)
}
Enter fullscreen mode Exit fullscreen mode

Input Example:

Alice 25

Output:

Hello Alice, you are 25 years old.
Enter fullscreen mode Exit fullscreen mode

4. bufio.NewReader (For Advanced Input Handling)

  • Purpose: Provides more advanced input reading capabilities compared to fmt.
  • Stops Reading: Allows reading an entire line including spaces.
  • Multiple Variables: Reads input as a single string and then can be split further if needed.
  • Returns: The complete input line as a string.

Example:

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter your name and age: ")
    input, _ := reader.ReadString('\n') // Reads entire line including spaces
    input = strings.TrimSpace(input)    // Trim newline and spaces
    fmt.Printf("You entered: %s\n", input)
}
Enter fullscreen mode Exit fullscreen mode

Input Example:

Alice 25

Output:

You entered: Alice 25
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Summary Table:

Function Purpose Stops Reading At Supports Formatting? Multiple Variables? Use Case
fmt.Scan Basic scanning Whitespace ❌ βœ… Simple input without newline
fmt.Scanln Scans until newline Newline (\n) ❌ βœ… Input until newline
fmt.Scanf Formatted input scanning Controlled by format βœ… βœ… Precise formatted input
bufio.NewReader Advanced input handling Customizable βœ… ❌ Large input with spaces

Top comments (0)