Go fmt.Sprintf format string


Release date:2023-09-08 Update date:2023-10-13 Editor:admin View counts:407

Label:

Go fmt.Sprintf format string

Go can be used fmt.Sprintf to format the string in the following format:

fmt.Sprintf(Format style, parameter list…)
  • Format style: in the form of a string, formatting symbols to % at the beginning %s string format, %d decimal integer format.

  • Parameter list: multiple parameters are separated by commas, and the number must correspond to the number in the formatting style one by one, otherwise the runtime will report an error.

Example

package main
import (
    "fmt"
    "io"
    "os"
)
func main() {
    // Format the string in go and assign 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 instance,% 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, ignore some errors
}

The output is as follows:

Code=000987&endDate=2020-12-31
Kim is 22 years old.

Go string formatting symbol:

Format

Description

%v

Output by the original value of the value

%+v

Expand the structure field name and value on the basis of%v

%#v

Output values in Go language syntax format

%T

Output the types and values of the Go language syntax format

%%

Output% ontology

%b

The integer is displayed in binary mode

%o

Integers are displayed in octal mode

%d

Integers are displayed in decimal mode

%x

The integer is displayed in hexadecimal mode

%X

Integers are displayed in hexadecimal, uppercase letters

%U

Unicode character

%f

Floating point number

%p

Pointer, displayed in hexadecimal mode

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")
}

The output is as follows:

{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

Powered by TorCMS (https://github.com/bukun/TorCMS).