Table of Contents
- Introduction
- Prerequisites
- Overview
- Reading Standard Input
- Writing to Standard Output
- Real-World Example
- Conclusion
Introduction
In this tutorial, we will explore how to work with standard input and standard output in Go. We will learn how to read input from the user and how to write output to the console. Standard input refers to the source from which a program receives its input, and standard output is the standard destination for a program’s output.
By the end of this tutorial, you will understand how to interact with the user through terminal input and output in your Go programs.
Prerequisites
To follow along with this tutorial, you should have a basic understanding of the Go programming language. You should have Go installed on your machine and have a text editor or integrated development environment (IDE) set up to write and run Go code.
Overview
Go provides the fmt
package to handle reading from standard input and writing to standard output. This package offers different functions for reading and writing data in various formats.
Reading Standard Input
To read input from the user, we can use the Scan
function from the fmt
package. The Scan
function takes a pointer to the variable where the input will be stored.
Here’s an example of reading a string from the user:
package main
import "fmt"
func main() {
var name string
fmt.Print("Enter your name: ")
fmt.Scan(&name)
fmt.Println("Hello, " + name + "!")
}
In this example, we first declare a variable name
of type string. We then use the fmt.Print
function to display a prompt to the user. Next, we use the fmt.Scan
function to read the user’s input and store it in the name
variable. Finally, we use the fmt.Println
function to print a greeting message to the user.
You can run this program and enter your name when prompted. The program will then greet you with a personalized message.
Writing to Standard Output
To write output to the console, we can use the Print
and Println
functions from the fmt
package. The Print
function writes the given message to the standard output without appending a newline character, while Println
appends a newline character after the message.
Here’s an example of writing a message to the standard output:
package main
import "fmt"
func main() {
fmt.Print("This is a message.")
fmt.Println("This is another message.")
}
In this example, we use the fmt.Print
function to write the message “This is a message” to the console. Since Print
does not append a newline character, the cursor remains on the same line. Next, we use the fmt.Println
function to write the message “This is another message” to the console. This time, Println
adds a newline character, so the cursor moves to the next line.
You can run this program to see the output on your console.
Real-World Example
Now, let’s see a real-world example of working with standard input and output in Go. Consider a program that calculates the area and perimeter of a rectangle. We will prompt the user to enter the length and width of the rectangle and then calculate and display the area and perimeter.
Here’s the code for this program:
package main
import "fmt"
func main() {
var length, width float64
fmt.Print("Enter length: ")
fmt.Scan(&length)
fmt.Print("Enter width: ")
fmt.Scan(&width)
area := length * width
perimeter := 2 * (length + width)
fmt.Printf("Area: %.2f\n", area)
fmt.Printf("Perimeter: %.2f\n", perimeter)
}
In this program, we declare variables length
and width
to store the user’s input. We prompt the user to enter the length and width using fmt.Print
, and then we read the values into the respective variables using fmt.Scan
.
Next, we calculate the area by multiplying the length and width, and the perimeter by using the formula 2 * (length + width). Finally, we use fmt.Printf
to format the output and display the calculated area and perimeter.
You can run this program and enter the length and width when prompted. The program will then calculate and display the area and perimeter of the rectangle.
Conclusion
In this tutorial, we learned how to work with standard input and standard output in Go. We saw how to read input from the user using the Scan
function and how to write output to the console using the Print
and Println
functions. We also went through a real-world example to calculate the area and perimeter of a rectangle.
By understanding how to interact with standard input and output in Go, you can create programs that accept user input and provide meaningful output, making your applications more interactive and engaging.