Go fmt.Printf format strings

Go Language Basic Syntax

fmt.Printf is a powerful output formatting function in the Go language, mainly used to format strings and output the result to standard output (usually the console).

fmt.Printf formats subsequent variables according to the specified format string; the syntax is as follows:

fmt.Printf(格式化样式, 参数列表…)
  • Formatting style:String form, formatting symbols begin with%Starts with %; %s is string format, %d is decimal integer format.
  • Parameter list:Multiple parameters are separated by commas, and the number must correspond one-to-one with the number in the format string, otherwise an error will be reported at runtime.

Example

package main

import (
   "fmt"
   "strings"
)


func main() {
  name := "John Doe"
  age := 27

  // Print the string in lowercase
  fmt.Printf("hello, %s\n", strings.ToLower(name))

  // Print the string in uppercase
  fmt.Printf("hello, %s\n", strings.ToUpper(name))

  // Print age in decimal
  fmt.Printf("%s is %d years old\n", name, age)

  // Age printed as a floating-point number with two decimal places
  fmt.Printf("%s is %.2f years old\n", name, age)
}

Output result:

hello, John Doe
hello, JOHN DOE
John Doe is 27 years old
John Doe is %!f(int=27) years old

Go string formatting symbols:

Format specifierDescriptionExample
General format
%vOutput the variable in default formatfmt.Printf("%v", 42)
%+vOutput struct with field namesfmt.Printf("%+v", struct{A int}{A: 42})
%#vFormat output in Go syntaxfmt.Printf("%#v", map[string]int{"a": 1})
%TType of output valuefmt.Printf("%T", 42)
%%Output percent signfmt.Printf("%%")
Boolean value
%tOutputtrueorfalsefmt.Printf("%t", true)
Integer
%bBinary representationfmt.Printf("%b", 5)
%cUnicode corresponding characterfmt.Printf("%c", 65)
%dDecimal representationfmt.Printf("%d", 42)
%oOctal representationfmt.Printf("%o", 10)
%xHexadecimal representation (lowercase letters)fmt.Printf("%x", 15)
%XHexadecimal representation (uppercase letters)fmt.Printf("%X", 15)
%UUnicode format outputfmt.Printf("%U", 65)
Floating-point number
%fDecimal floating-point numberfmt.Printf("%f", 3.14)
%eScientific notation (lowercase e)fmt.Printf("%e", 3.14)
%EScientific notation (uppercase E)fmt.Printf("%E", 3.14)
%gAutomatic selection%for%eConcise representationfmt.Printf("%g", 3.14)
Strings and bytes
%sPlain stringfmt.Printf("%s", "Go")
%qString or character with double quotesfmt.Printf("%q", "Go")
%xEach byte represented as two hexadecimal charactersfmt.Printf("%x", "abc")
%XHexadecimal (uppercase) representationfmt.Printf("%X", "abc")
Pointer
%pPointer addressfmt.Printf("%p", &x)

The format string consists of regular text and format placeholders.

Formatting placeholder with% Starts with %, followed by one or more characters that specify the format type.

The structure of a formatting placeholder is:

%[flags][width][.precision]verb
  • flagsFlags used to control formatted output (optional).
    • -: Left-aligned.
    • +: Always display the sign of the value.
    • 0: padded with zeros.
    • #Adds a prefix for binary, octal, hexadecimal, etc.
    • Space: Positive numbers preceded by a space, negative numbers preceded by-。
  • width: Output width (optional).
  • .precisionNumber of digits after the decimal point for floating-point numbers (optional).
  • verb: Used to specify the formatting method of the data.

1. Formatting integers

fmt.Printf("十进制: %d, 二进制: %b, 八进制: %o, 十六进制: %x\n", 42, 42, 42, 42)
// 输出:十进制: 42, 二进制: 101010, 八进制: 52, 十六进制: 2a

2. Formatting floating-point numbers

fmt.Printf("普通: %f, 科学计数法: %e, 自动: %g\n", 123.456, 123.456, 123.456)
// 输出:普通: 123.456000, 科学计数法: 1.234560e+02, 自动: 123.456

3. Formatting strings

fmt.Printf("普通字符串: %s, 带引号: %q, 十六进制: %x\n", "Go", "Go", "Go")
// 输出:普通字符串: Go, 带引号: "Go", 十六进制: 476f

4. Using width and alignment

fmt.Printf("|%10s|%-10s|\n", "right", "left")
// 输出:|     right|left      |

5. Formatting structs

type User struct {
    Name string
    Age  int
}
user := User{Name: "Alice", Age: 30}
fmt.Printf("默认: %v\n带字段名: %+v\nGo语法: %#v\n", user, user, user)
// 输出:
// 默认: {Alice 30}
// 带字段名: {Name:Alice Age:30}
// Go语法: main.User{Name:"Alice", Age:30}

6. Formatting boolean values

fmt.Printf("布尔值: %t, %t\n", true, false)
// 输出:布尔值: true, false

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 Language Basic Syntax

other extensions