🎓 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
In this tutorial, we will learn different ways to read input from the User or Console in the Golang program.
Go ( Golang) - Read Input from User or Console
- Go - read input with Scanf
- Go - read input from the user with NewReader
- Go - read input from the user with NewScanner
Go - read input with Scanf
The Scanf function scans text read from standard input, storing successive space-separated values into successive arguments as determined by the format. It returns the number of items successfully scanned.
Let's create a file named go_example.go and add the following content to it:
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter your name: ")
fmt.Scanf("%s", &name)
fmt.Println("Hello", name)
}
Output:
G:\GoLang\examples>go run go_example.go Enter your name: John Hello John
The example prompts the user to enter his name.
Go - read input from the user with NewReader
The program reads a name from the input using bufio.NewReader.
Let's create a file named go_example.go and add the following content to it:
package main
import (
"os"
"bufio"
"fmt"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter your name: ")
name, _ := reader.ReadString('\n')
fmt.Printf("Hello %s\n", name)
}
Output:
G:\GoLang\examples>go run go_example.go Enter your name: John Hello John
Go - read input from the user with NewScanner
The Scanner provides a convenient interface for reading data such as a file of newline-delimited lines of text.
Below example of read input from a user with NewScanner in the Go language.
Let's create a file named go_example.go and add the following content to it:
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
names := make([]string, 0)
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("Enter name: ")
scanner.Scan()
text := scanner.Text()
if len(text) != 0 {
fmt.Println(text)
names = append(names, text)
} else {
break
}
}
fmt.Println(names)
}
Output:
G:\GoLang\examples>go run go_example.go Enter name: John John Enter name: Tony Tony Enter name: Tom Tom Enter name: [John Tony Tom]
Note: The example allows to read multiple names from the user. Press enter without value to finish the program.
Golang Related Tutorials
- Go (Golang) Functions with Examples
- Go (Golang) Operators with Examples
- Go ( Golang) - Read Input from User or Console
- Go (Golang) Read and Write File Example Tutorial
- Go (Golang) Array Tutorial
- Go (Golang) Slices Tutorial with Examples
- Go (Golang) Maps Tutorial with Examples
- Go (Golang) Structs Tutorial with Examples
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