Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is the golang Scanf function?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Scanf function in the Go programming language is used to read data from the standard input, format it into a string, and store the resultant strings into the destinations specified by a list of additional arguments you provide. It differs from the simple Scan function as it supports custom format specifiers and uses a format string to format the output string. In contrast, in Scan, only default formats are used to format the string.

To use this function, you must import the fmt package in your file and access the Scanf function within by using the . notation (fmt.Scanf). Here, Scanf is the actual function, while fmt is the Go package that stores the definition of this function.

Function definition

The definition of the Scanf function inside the fmt package is:

Parameters

fmt.Scanf takes a format string along with a list of a variable number of arguments.

  • format: This argument is of type string and represents the string containing custom specifiers that the Scanf uses to format the final output string.

  • a ...interface{}: The list of all arguments where you want to store data. After taking in the input, the input string is automatically split up on space characters. The components are stored sequentially into the given arguments. If there are fewer arguments than the different splits of the input string, then the extra pieces are discarded.

Common specifiers

The following is a table of the most commonly used format specifiers in Go and their descriptions.

Specifiers Description
%s To print a string
%d To print an integer
%v To print values of all elements in a structure
%+v To print the names and values of all elements in a structure

Return values

The fmt.Scanf function can return two things:

  • count: The number of arguments the function writes to.

  • err: Any error thrown during the execution of the function.

Example

In the following example, we use the Scanf function to take input from standard input and store the data in it to several other variables.

Scanf reads from the input source sequentially. Hence, we must give the list of arguments in the order specified in the format string.

Here, we require an input with the specific format matching the sequence of arguments we have given the Scanf function.

For example, with the first word being a string, the second can be anything as it is stored in temp and is unused. The third can be an int with another string after that.

An example input would be, “Faraz owns 500 acres of land”. We then use the Scanf function to read this input and store parts of the corresponding string to the amount of landunits of measurement and the owner’s name. We then use Printf to print a new string.

Enter input, then press RUN.

package main
import (
"fmt"
)
func main() {
// the message we try here would be:
// Faraz owns 500 acres of land
// or something follwoing the same format
var name string
var unit string
var amount int
var temp string
// taking input and storing in variable using the buffer string
fmt.Scanf("%s %s %d %s", &name, &temp, &amount, &unit)
// print out new string using the extracted values
fmt.Printf ("%d %s of land is owned by %s\n",amount, unit, name);
}

Enter the input below

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring