Home >Backend Development >Golang >How to Scan in GoLang

How to Scan in GoLang

Susan Sarandon
Susan SarandonOriginal
2025-01-05 14:51:44496browse

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)
}

Input Example:

Alice 25

Output:

Hello Alice, you are 25 years old.

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)
}

Input Example:

Alice 25

Output:

Hello Alice, you are 25 years old.

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)
}

Input Example:

Alice 25

Output:

Hello Alice, you are 25 years old.

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)
}

Input Example:

Alice 25

Output:

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)
}

? 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
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

The above is the detailed content of How to Scan in GoLang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn