Golang fmt Scanln Function

The fmt.Scanln function in Golang is part of the fmt package and is used to read input from the standard input (usually the console) until a newline is encountered. It scans space-separated values and assigns them to the specified variables. It stops reading once it encounters a newline character, making it suitable for reading full lines of input.

Table of Contents

  1. Introduction
  2. Scanln Function Syntax
  3. Examples
    • Basic Usage
    • Reading Multiple Values
  4. Real-World Use Case
  5. Conclusion

Introduction

The fmt.Scanln function is used to capture user input from the console line by line. It reads input values separated by spaces and assigns them to the provided variables, stopping at the newline character. This makes it useful for applications where input is entered line by line, such as command-line tools or interactive applications.

Scanln Function Syntax

The syntax for the fmt.Scanln function is as follows:

func Scanln(a ...interface{}) (n int, err error)

Parameters:

  • a: Pointers to variables where the scanned data will be stored. Each variable should correspond to the expected input type.

Returns:

  • n: The number of items successfully scanned and assigned.
  • err: An error if one occurred during scanning.

Examples

Basic Usage

This example demonstrates how to use the fmt.Scanln function to read a single line of input from the user.

Example

package main

import (
	"fmt"
)

func main() {
	var name string

	// Prompt the user for input
	fmt.Print("Enter your name: ")

	// Use fmt.Scanln to read a single line of input
	fmt.Scanln(&name)

	// Print the captured input
	fmt.Println("Hello,", name)
}

Console Input/Output:

Enter your name: Alice
Hello, Alice

Reading Multiple Values

You can use fmt.Scanln to read multiple space-separated values from a single line of input.

Example

package main

import (
	"fmt"
)

func main() {
	var name string
	var age int

	// Prompt the user for input
	fmt.Print("Enter your name and age: ")

	// Use fmt.Scanln to read multiple values from a single line
	fmt.Scanln(&name, &age)

	// Print the captured input
	fmt.Printf("Name: %s, Age: %d\n", name, age)
}

Console Input/Output:

Enter your name and age: Bob 25
Name: Bob, Age: 25

Real-World Use Case

Interactive Command-Line Applications

In real-world applications, fmt.Scanln can be used to read user commands and inputs for interactive command-line applications.

Example

package main

import (
	"fmt"
)

func main() {
	var command string

	for {
		// Prompt the user for a command
		fmt.Print("Enter a command (start/stop/exit): ")

		// Use fmt.Scanln to read the command
		fmt.Scanln(&command)

		// Handle the command
		switch command {
		case "start":
			fmt.Println("Starting the process...")
		case "stop":
			fmt.Println("Stopping the process...")
		case "exit":
			fmt.Println("Exiting...")
			return
		default:
			fmt.Println("Unknown command. Please enter 'start', 'stop', or 'exit'.")
		}
	}
}

Console Input/Output:

Enter a command (start/stop/exit): start
Starting the process...
Enter a command (start/stop/exit): stop
Stopping the process...
Enter a command (start/stop/exit): exit
Exiting...

Conclusion

The fmt.Scanln function is a straightforward way to read input line by line from the console in Go. It allows you to capture space-separated values and stop reading at the newline character, making it ideal for interactive applications and command-line tools. By using fmt.Scanln, you can efficiently handle user input in your Go programs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare