ADVERTISEMENT
ADVERTISEMENT

Golang fmt.Sscanln() Function with Examples

Golang | fmt.Sscanln() Function: Here, we are going to learn about the Sscanln() function of the fmt package with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 10, 2021

fmt.Sscanln()

In Go language, the fmt package implements formatted I/O with functions analogous to C's printf() and scanf(). The Sscanln() function is an inbuilt function of the fmt package. The Sscanln() function is similar to Sscan() function, but stops scanning at a newline and after the final items there must be a newline or EOF.

It accepts two parameters (str string, a ...interface{}) and returns the number of total items successfully parsed and an error if occurred during paring.

Syntax:

func Sscanln(str string, a ...interface{}) (n int, err error)

Parameter(s):

  • str : It contains the argument string to be extracted.
  • a : A custom type that is used to specify a set of one or more method signatures, here we can provide a set of the variables, constants, functions, etc.

Return Value:

The return type of the fmt.Sscanln() function is a string, it returns the number of total items successfully parsed and an error if occurred during paring.

Example 1:

// Golang program to demonstrate the
// example of fmt.Sscanln() function

package main

import (
	"fmt"
)

func main() {
	var x int
	var y int

	// Scans the values (space-separated)
	// and assigns to the variables
	fmt.Sscanln("10 20\n", &x, &y)

	// Prints the values
	fmt.Println("x:", x)
	fmt.Println("y:", y)

	// Scans the values (multi space-separated)
	// and assigns to the variables
	fmt.Sscanln("30      40\n", &x, &y)

	// Prints the values
	fmt.Println("x:", x)
	fmt.Println("y:", y)

	// Scans the values (Tab-separated)
	// and assigns to the variables
	fmt.Sscanln("50\t60\n", &x, &y)

	// Prints the values
	fmt.Println("x:", x)
	fmt.Println("y:", y)
}

Output:

x: 10
y: 20
x: 30
y: 40
x: 50
y: 60

Example 2:

// Golang program to demonstrate the
// example of fmt.Sscanln() function

package main

import (
	"fmt"
)

func main() {
	// Assign the value to a string variable
	data := "Alex 21 98.50\n"

	// Declare the variables to store the values
	var name string
	var age int
	var perc float32

	// Scans the values and assigns
	// to the variables
	fmt.Sscanln(data, &name, &age, &perc)

	// Prints the values
	fmt.Println("Name:", name)
	fmt.Println("Age:", age)
	fmt.Println("Percentage:", perc)
}

Output:

Name: Alex
Age: 21
Percentage: 98.5

Golang fmt Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.