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, this function is defined under the fmt package. Here, you need to import the “fmt” package in order to use these functions.
Syntax:
func Sscanf(str string, format string, a ...interface{}) (n int, err error)
Parameters: This function accepts three parameters which are illustrated below:
- str string: This parameter contains the specified text which is going to be scanned.
- format string: This parameter is the different types of format for each elements of the specified string.
- a …interface{}: This parameter receives each elements of the string.
Returns: It returns the number of items successfully parsed.
Example 1:
// Golang program to illustrate the usage of // fmt.Sscanf() function // Including the main package package main // Importing fmt import ( "fmt") // Calling main func main() { // Declaring two variables var name string var alphabet_count int // Calling the Sscanf() function which // returns the number of elements // successfully parsed and error if // it persists n, err := fmt.Sscanf("GFG is having 3 alphabets.", "%s is having %d alphabets.", &name;, &alphabet;_count) // Below statements get // executed if there is any error if err != nil { panic(err) } // Printing the number of // elements and each elements also fmt.Printf("%d: %s, %d\n", n, name, alphabet_count) } |
Output:
2: GFG, 3
Example 2:
// Golang program to illustrate the usage of // fmt.Sscanf() 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 var float_value float32 var boolean_value bool // Calling the Sscanf() function which // returns the number of elements // successfully parsed and error if // it persists n, err := fmt.Sscanf("GeeksforGeeks 13 6.7 true", "%s %d %g %t", &name;, &alphabet;_count, &float;_value, &boolean;_value) // Below statements get executed // if there is any error if err != nil { panic(err) } // Printing the number of elements // and each elements also fmt.Printf("%d: %s, %d, %g, %t", n, name, alphabet_count, float_value, boolean_value) } |
Output:
4: GeeksforGeeks, 13, 6.7, true
Recommended Posts:
- complx.NaN() Function in Golang With Examples
- fmt.Errorf() Function in Golang With Examples
- bits.Add() Function in Golang with Examples
- reflect.Set() Function in Golang with Examples
- reflect.Len() Function in Golang with Examples
- fmt.Fprint() Function in Golang With Examples
- fmt.Println() Function in Golang With Examples
- fmt.Printf() Function in Golang With Examples
- fmt.print() Function in Golang With Examples
- fmt.Fprintln() Function in Golang With Examples
- fmt.Fprintf() Function in Golang With Examples
- complx.Exp() Function in Golang With Examples
- io.NewSectionReader() Function in Golang with Examples
- io.Copy() Function in Golang with Examples
- bits.Len() Function in Golang with Examples
- io.CopyBuffer() Function in Golang with Examples
- complx.Pow() Function in Golang With Examples
- reflect.Cap() Function in Golang with Examples
- math.Inf() Function in Golang With Examples
- math.J0() Function in Golang With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

