fmt.Sscanln() Function in Golang With Examples
Last Updated :
05 May, 2020
In Go language,
fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The
fmt.Sscanln() function in Go language scans the specified string and stores the successive space-separated values into successive arguments. This function stops scanning at a newline and after the final item, there must be a newline or EOF. Moreover, this function is defined under the fmt package. Here, you need to import the "fmt" package in order to use these functions.
Syntax:
func Sscanln(str string, a ...interface{}) (n int, err error)
Parameters: This function accepts two parameters which are illustrated below:
- str string: This parameter contains the specified text which is going to be scanned.
- a ...interface{}: This parameter receives each elements of the string.
Returns: It returns the number of items successfully scanned.
Example 1:
C
// Golang program to illustrate the usage of
// fmt.Sscanln() function
// Including the main package
package main
// Importing fmt
import (
"fmt"
)
// Calling main
func main() {
// Declaring some variables
var name string
var alphabet_count int
// Calling Sscanln() function
n, err := fmt.Sscanln("GFG 3", &name, &alphabet_count)
// Checking if the function
// returns any error
if err != nil {
panic(err)
}
// Printing the number of elements
// present in the specified string
// and also the elements
fmt.Printf("n: %d, name: %s, alphabet_count: %d",
n, name, alphabet_count)
}
Output:
n: 2, name: GFG, alphabet_count: 3
Example 2:
C
// Golang program to illustrate the usage of
// fmt.Sscanln() function
// Including the main package
package main
// Importing fmt
import (
"fmt"
)
// Calling main
func main() {
// Declaring some variables
var name string
var alphabet_count int
// Calling Sscanln() function
fmt.Sscanln("GFG \n 3", &name, &alphabet_count)
// Printing the elements of the string
fmt.Printf("name: %s, alphabet_count: %d", name, alphabet_count)
}
Output:
name: GFG, alphabet_count: 0
In the above example, it can be seen that the assigned value of alphabet_count was 3 still the output is 0 this is because of there is new line (\n) in between two elements "GFG" and "alphabet_count" and hence this function stop scanning at a newline.
Similar Reads
fmt.Scanln() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Scanln() function in Go language scans the input texts which is given in the standard input, reads from there and stores the successive space-separated values into successive a
2 min read
fmt.Sscan() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Sscan() function in Go language scans the specified texts and store the successive space-separated texts into successive arguments. Moreover, this function is defined under the
2 min read
fmt.Sscanf() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Sscanf() function in Go language scans the specified string and stores the successive space-separated values into successive arguments as determined by the format. Moreover, th
2 min read
fmt.Scan() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Scan() function in Go language scans the input texts which is given in the standard input, reads from there and stores the successive space-separated values into successive arg
2 min read
fmt.Scanf() Function in Golang With Examples In Go language, fmt package implements formatted I/O with functions analogous to C's printf() and scanf() function. The fmt.Scanf() function in Go language scans the input texts which is given in the standard input, reads from there and stores the successive space-separated values into successive ar
2 min read