Skip to content
Home » How to demonstrate the fmt.Sscanf() function in Golang

How to demonstrate the fmt.Sscanf() function in Golang

Learn about How to demonstrate the fmt.Sscanf() function in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to demonstrate the fmt.Sscanf() function in Golang

// Golang program to demonstrate the
// fmt.Sscanf() function

package main

import "fmt"

func main() {
	var dd int = 0
	var mm int = 0
	var yy int = 0

	var strDate string = "17-04-20"

	_, err := fmt.Sscanf(strDate, "%02d-%02d-%02d", &dd, &mm, &yy)
	if err != nil {
		panic(err)
	}

	fmt.Println("DD: ", dd)
	fmt.Println("MM: ", mm)
	fmt.Println("YY: ", yy)
}

In the main function, we created three integer variables dd, mm, yy that are initialized with 0. Then we extract values for dd, mm, yy variables using fmt.Sscanf() function. After that, we printed the result on the console screen.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to find the largest number between two numbers in Golang
How to calculate the power of a number in Golang