Table of Contents
Introduction
In Go, the fmt
package provides functions for formatting input and output. Two commonly used functions in this package are Printf
and Scanf
. Printf
is used to format and print output, while Scanf
is used for reading formatted input. This tutorial will explore how to use Printf
and Scanf
in Go, and by the end, you will understand how to format and print output, as well as how to read input in a specific format.
Prerequisites
Before starting this tutorial, you should have a basic understanding of Go programming language fundamentals. It is recommended to have Go installed on your machine. If you haven’t installed Go yet, please follow the official Go installation guide for your operating system.
Setup
Once Go is installed and properly configured, you can create a new Go file using any text editor or an integrated development environment (IDE) of your choice. Save the file with a .go extension, for example, main.go
.
You are now ready to explore the Printf
and Scanf
functions.
Printf
The Printf
function is used to format and print output in Go. It takes a format string as its first argument, which specifies the desired format of the output, followed by an optional list of arguments. The format string can contain placeholders (%
) that will be replaced by the values of the corresponding arguments.
Here is a basic example that demonstrates the usage of Printf
:
package main
import "fmt"
func main() {
name := "John"
age := 25
fmt.Printf("My name is %s and I am %d years old.\n", name, age)
}
In the above example, we use %s
as a placeholder for a string and %d
as a placeholder for an integer. The values of name
and age
are passed as arguments to Printf
and printed accordingly.
You can use various formatting verbs to control the appearance of the output. Here are some common formatting verbs:
%d
- decimal (integer) representation%f
- floating-point representation%s
- string representation%t
- boolean representation%v
- default representation
You can also specify additional formatting options like width and precision. For example, %5d
specifies a width of 5 for a decimal representation.
Scanf
The Scanf
function is used to read formatted input in Go. It takes a format string as its first argument, which specifies the desired format of the input, followed by pointers to the variables where the values will be stored.
Here is a basic example that demonstrates the usage of Scanf
:
package main
import "fmt"
func main() {
var name string
var age int
fmt.Print("Enter your name: ")
fmt.Scanf("%s", &name)
fmt.Print("Enter your age: ")
fmt.Scanf("%d", &age)
fmt.Printf("Hello %s, you are %d years old.\n", name, age)
}
In the above example, we use %s
as a format specifier for a string input and %d
for an integer input. The Scanf
function reads the input from the standard input and stores it in the variables name
and age
.
Example
Let’s create a simple program that demonstrates the usage of Printf
and Scanf
together:
package main
import "fmt"
func main() {
var name string
var age int
fmt.Print("Enter your name: ")
fmt.Scanf("%s", &name)
fmt.Print("Enter your age: ")
fmt.Scanf("%d", &age)
fmt.Printf("Hello %s, you are %d years old.\n", name, age)
}
Save the above code in a file named main.go
and execute it using the go run
command:
go run main.go
You will be prompted to enter your name and age. After providing the input, the program will display a greeting with your name and age.
Conclusion
In this tutorial, you learned how to use the Printf
and Scanf
functions in Go. You can now format and print output using Printf
and read formatted input using Scanf
. Remember to provide the correct format specifiers for the type of input or output you are dealing with. Exploring the fmt
package further will reveal more advanced formatting options and functionalities. Happy coding!