🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
ScanlnFunction Syntax- Examples
- Basic Usage
- Reading Multiple Values
- Real-World Use Case
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment