fmt.Sscan() 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.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 fmt package. Here, you need to import the “fmt” package in order to use these functions.
Syntax:
func Sscan(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 texts.
Returns: It returns the number of items successfully scanned.
Example 1:
package main
import (
"fmt"
)
func main() {
var name string
var alphabet_count int
n, err := fmt.Sscan( "GFG 3" , &name, &alphabet_count)
if err != nil {
panic(err)
}
fmt.Printf( "%d: %s, %d\n" , n, name, alphabet_count)
}
|
Output:
2: GFG, 3
Example 2:
package main
import (
"fmt"
)
func main() {
var name string
var alphabet_count int
var float_value float32
var boolean_value bool
n, err := fmt.Sscan( "GeeksforGeeks 13 6.7 true" ,
&name, &alphabet_count, &float_value, &boolean_value)
if err != nil {
panic(err)
}
fmt.Printf( "%d: %s, %d, %g, %t" , n, name,
alphabet_count, float_value, boolean_value)
}
|
Output:
4: GeeksforGeeks, 13, 6.7, true
Share your thoughts in the comments
Please Login to comment...