In Go language, fmt package implements formatted I/O with functions analogous to C’s printf() and scanf() function. The fmt.Sprintf() function in Go language formats according to a format specifier and returns the resulting string. 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 Sprintf(format string, a ...interface{}) string
Parameters: This function accepts two parameters which are illustrated below:
- format string: This includes some varbs along with some strings.
- a …interface{}: This is the specified constant variables.
Returns: It returns the resulting string.
Example 1:
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, dept = "GeeksforGeeks" , "CS"
s := fmt.Sprintf( "%s is a %s Portal.\n" , name, dept)
io.WriteString(os.Stdout, s)
}
|
Output:
GeeksforGeeks is a CS Portal.
Example 2:
package main
import (
"fmt"
"io"
"os"
)
func main() {
const num1, num2, num3 = 5, 10, 15
s := fmt.Sprintf( "%d + %d = %d" , num1, num2, num3)
io.WriteString(os.Stdout, s)
}
|
Output:
5 + 10 = 15