Table of Contents
- Introduction
- Prerequisites
- Installing Go
- Understanding the Scan Function
- Working with the Scan Function
- Conclusion
Introduction
In this tutorial, we will explore the Scan function in Go. The Scan function is part of the fmt package and is used to read formatted input from the standard input (keyboard). By the end of this tutorial, you will have a clear understanding of how the Scan function works and how it can be used in your Go programs.
Prerequisites
Before you start this tutorial, you should have a basic understanding of Go programming language syntax and concepts. You should also have Go installed on your system.
Installing Go
If you haven’t installed Go yet, follow these steps to install it on your system:
- Visit the official Go website at https://golang.org/dl/.
- Download the appropriate installer for your operating system.
-
Run the installer and follow the installation instructions.
-
Verify the installation by opening a terminal or command prompt and running the command
go version. You should see the Go version number printed.Now that you have Go installed, let’s dive into understanding the
Scanfunction.
Understanding the Scan Function
The Scan function is defined in the fmt package and is used for reading input from the standard input. It scans the standard input for formatted text and assigns the values to the provided variables. It stops scanning when a newline character is encountered.
The Scan function has the following signature:
func Scan(a ...interface{}) (n int, err error)
The function takes one or more arguments, where each argument is a pointer to the variable in which the scanned value will be stored. It returns the number of items successfully scanned and an error, if any.
The Scan function uses a format string to determine the expected input format. The format string consists of verb codes, each preceded by a percent sign (%). Here are some common verb codes used with Scan:
%d- scans an integer value%f- scans a float value%s- scans a string value
Now, let’s see how to work with the Scan function in practice.
Working with the Scan Function
-
Create a new Go file called
main.goand open it in a text editor. -
Import the
fmtpackage by adding the following line at the beginning of the file:import "fmt" -
Declare variables for storing the scanned values. For example, if you want to scan an integer and a string, you can declare two variables as follows:
var num int var text string -
In the
mainfunction, use theScanfunction to read the values. Pass the addresses of the variables as arguments toScan:func main() { fmt.Print("Enter an integer: ") fmt.Scan(&num) // Scans an integer and stores in 'num' fmt.Print("Enter a string: ") fmt.Scan(&text) // Scans a string and stores in 'text' fmt.Printf("You entered: %d and %s\n", num, text) } -
Save the file and compile it using the command
go build main.go. -
Run the compiled executable using the command
./main(ormain.exeon Windows). -
Enter an integer and a string when prompted. Press Enter after each input.
-
The program will display the entered values on the console.
Congratulations! You have successfully used the
Scanfunction to read and store values from the standard input.
Conclusion
In this tutorial, you learned about the Scan function in Go. We covered its purpose, usage, and how to work with it to read formatted input from the standard input. Now you can use the Scan function to interactively read user input and process it in your Go programs. Experiment with different data types and format strings to expand your understanding of the Scan function’s capabilities.
Remember to check the Go documentation for more details and additional functions provided by the fmt package.
Happy coding in Go!