Go fmt.Sprintf String Formatting
In Go, you can use fmt.Sprintf
to format strings, following this format:
fmt.Sprintf(format pattern, argument list...)
Format Pattern: A string pattern where format specifiers start with
%
, such as%s
for string format and%d
for decimal integer format.Argument List: Multiple arguments separated by commas, which must correspond in number to the specifiers in the format pattern, otherwise, an error will occur at runtime.
Example
package main
import (
"fmt"
"io"
"os"
)
func main() {
// Formatting a string in Go and assigning it to a new string using fmt.Sprintf
// %s represents a string
var stockcode = "000987"
var enddate = "2020-12-31"
var url = "Code=%s&endDate=%s"
var target_url = fmt.Sprintf(url, stockcode, enddate)
fmt.Println(target_url)
// Another example, %d represents an integer
const name, age = "Kim", 22
s := fmt.Sprintf("%s is %d years old.\n", name, age)
io.WriteString(os.Stdout, s) // For simplicity, ignoring some errors
}
Output:
Code=000987&endDate=2020-12-31
Kim is 22 years old.
Example
package main
import (
"fmt"
"io"
"os"
)
func main() {
// Declaring numeric variables
const num1, num2, num3 = 5, 10, 15
// Calling the Sprintf() function
s := fmt.Sprintf("%d + %d = %d", num1, num2, num3)
// Using the WriteString() function to output the result to the terminal
io.WriteString(os.Stdout, s)
}
Output:
5 + 10 = 15
Go String Format Specifiers:
Format | Description |
---|---|
%v | Default format for the value |
%+v | Adds field names to the default format |
%#v | A Go-syntax representation of the value |
%T | A Go-syntax representation of the type of the value |
%% | A literal percent sign |
%b | Binary representation for integers |
%o | Octal representation for integers |
%d | Decimal representation for integers |
%x | Hexadecimal representation for integers |
%X | Hexadecimal representation with uppercase letters for integers |
%U | Unicode format |
%f | Floating-point number |
%p | Pointer representation in hexadecimal |
Example
package main
import (
"fmt"
"os"
)
type point struct {
x, y int
}
func main() {
p := point{1, 2}
fmt.Printf("%v\n", p)
fmt.Printf("%+v\n", p)
fmt.Printf("%#v\n", p)
fmt.Printf("%T\n", p)
fmt.Printf("%t\n", true)
fmt.Printf("%d\n", 123)
fmt.Printf("%b\n", 14)
fmt.Printf("%c\n", 33)
fmt.Printf("%x\n", 456)
fmt.Printf("%f\n", 78.9)
fmt.Printf("%e\n", 123400000.0)
fmt.Printf("%E\n", 123400000.0)
fmt.Printf("%s\n", "\"string\"")
fmt.Printf("%q\n", "\"string\"")
fmt.Printf("%x\n", "hex this")
fmt.Printf("%p\n", &p)
fmt.Printf("|%6d|%6d|\n", 12, 345)
}
fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)
fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)
fmt.Printf("|%6s|%6s|\n", "foo", "b")
fmt.Printf("|%-6s|%-6s|\n", "foo", "b")
s := fmt.Sprintf("a %s", "string")
fmt.Println(s)
fmt.Fprintf(os.Stderr, "an %s\n", "error")
Output result:
{1 2}
{x:1 y:2}
main.point{x:1, y:2}
main.point
true
123
1110
!
1c8
78.900000
1.234000e+08
1.234000E+08
"string"
"\"string\""
6865782074686973
0xc0000b4010
| 12| 345|
| 1.20| 3.45|
|1.20 |3.45 |
| foo| b|
|foo |b |
a string
an error
Go Basic Syntax ```