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

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

Learn about How to demonstrate the fmt.Sprintf() 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.Sprintf() function in Golang

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

package main

import "fmt"

func main() {
	var dd int = 17
	var mm int = 04
	var yy int = 2021

	var str string

	str = fmt.Sprintf("%02d-%02d-%04d", dd, mm, yy)

	fmt.Println(str)
}

In the main() function, we created three integer variables dd, mm, yy respectively. Then we created a string with all variables using fmt.Sprintf() function.

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 demonstrate the fmt.Sscanf() function in Golang
How to find the largest number between two numbers in Golang