Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is the Sscanln function in golang?

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 Sscanln function in the Go programming language is used to read data from a string , of your choosing, and store resulting space-separated values into the destinations specified by a list of additional arguments.

This function differs from the simple Sscan function because it stops the scanning process when it encounters an EOF or newline character.

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

Function definition

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

Parameters

fmt.Sscanln takes a reading source along with a list of a variable number of arguments.

  • src: The from where the input is to be taken. This should be an object of type string.

  • a ...interface{}: The list of all arguments that you want to store data in. After taking in the input, the input string is automatically split 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.

Return values

The fmt.Sscanln function can return two things:

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

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

Examples

The following example is a simple program where we read data from the variable message of type string, store the space-separated parts into our desired variables, and then use the normal Print function to print a new string, using these variables, to the standard output.

Sscanln 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 that matches the sequence of arguments we have given the Sscanln function.

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

Here, we are using: “Faraz owns 500 acres of land”. We then use the Sscanln function to read this input and store parts of the string corresponding to the amount of land, units of measurement, and the owner’s name. Finally, we use Printf to print a new string.

package main
import (
"fmt"
)
func main() {
//assigning our buffer a message
message := "Faraz owns 500 acres of land"
var name string
var unit string
var amount int
var temp string
// taking input and storing in variable using the buffer string
fmt.Sscanln(message, &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);
}

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