SoFunction
Updated on 2025-03-05

Deep in the formatted output in Golang

fmt

The commonly used standard library for Go language to control text output is fmt

The main functions used for output in fmt are:

  • Print: Output to the console, no formatting operations are accepted
  • Println: Output to the console and wrap the line
  • Printf: Format output, you can only print out formatted strings, and you can only output variables of string type directly (you cannot directly output other types)
  • Sprintf: Format and return a string without any output
  • Fprintf: to format and output to instead

format

Use the Printf function to test the string formatting in Go language:

(Format style, parameter list…)

Format style: string form, formatting symbols start with %, %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 format style one by one, otherwise an error will be reported during runtime.

for example:

username := "boy"
("welcome, %s", username)

Integer formatting

Placeholder describe
%b Integers are displayed in binary
%o Integers are displayed in octal
%d Integers are displayed in decimal
%x Integers are displayed in hexadecimal
%X Integers are displayed in hexadecimal and letter capitalization
%c Characters represented by the corresponding Unicode code point
%U Unicode characters, Unicode format: 123, equivalent to "U+007B"
func main() {
    ("%b \n", 123) //1111011
    ("%o \n", 123) //173
    ("%d \n", 123) //123
    ("%x \n", 123) //7b
    ("%X \n", 123) //7B   
    ("%c \n", 123) //{
    ("%U \n", 123) //U+007B 
}

Floating point number formatting

Placeholder describe
%e Scientific notation method, e.g. 1.234560e+02
%E Scientific notation method, e.g. 1.234560E+02
%f There are decimal points but no exponents, for example 123.456
%F Equivalent to %f
%g Select %e or %f according to the situation to produce a more compact (0 without end) output
%G Select %E or %F according to the situation to produce a more compact (0 without end) output
func main() {
    ("%e \n", 123.456) //1.234560e+02
    ("%E \n", 123.456) //1.234560E+02
    ("%f \n", 123.456) //123.456000
    ("%F \n", 123.456) //123.456000
    ("%g \n", 123.456) //123.456
    ("%G \n", 123.456) //123.456 
}

Boolean type formatting

Placeholder describe
%t true or false
func main() {
    ("%t", true) //true
}

Character formatting

Placeholder describe
%c Characters represented by the corresponding Unicode code point
func main() {
    ("%c", 0x4E2D) //middle}

String formatting

Placeholder describe
%s Direct output string or []byte
%q String surrounded by double quotes, safely escaped by Go syntax
%x Each byte is represented by a two-character hexadecimal number (using a-f)
%X Each byte is represented by a two-character hexadecimal number (using A-F)
func main() {
    ("%s \n", "Hello world") //Hello world
    ("%q \n", "Hello world") //"Hello world"
    ("%x \n", "Hello world") //48656c6c6f20776f726c64
    ("%X \n", "Hello world") //48656C6C6F20776F726C64
}

Pointer Format

Placeholder describe
%p Denoted in hexadecimal with leading 0x
%#p Denoted in hexadecimal, with no leading 0x
func main() {
    a := "Hello world"
    b := &a
    ("%p \n", b)  //0xc000046230
    ("%#p \n", b) //c000046230
}

Universal placeholder

Placeholder describe
%v Default format of values
%+v Similar to %v, but field names will be added when outputting the structure
%#v Go syntax representation of corresponding values
%T Go syntax representation of the corresponding value type
%% Percent sign, literal %, non-placeholder meaning
func main() {
    ("%v \n", "Hello World")   //Hello World
    ("%+v \n", "Hello World")  //Hello World
    ("%#v \n", "Hello World")  //"Hello World"
    ("%T \n", "Hello World")   //string
    ("%%%v \n", "Hello World") //%Hello World
}

Width representation

Floating point precision control

The width is specified by a decimal number immediately following the percent sign. If the width is not specified, the value is not filled except necessary.

The accuracy is specified by a decimal number followed by a (optional) width followed by a dot number. If no precision is specified, the default precision will be used; if the dot is not followed by a number, the precision is 0. For example,

func main() {
    ("|%f|\n", 123.456)     //|123.456000|
    ("|%12f|\n", 123.456)   //|  123.456000|
    ("|%.3f|\n", 123.456)   //|123.456|
    ("|%12.3f|\n", 123.456) //|     123.456|
    ("|%|\n", 123.456)  //|         123|
}

String length control

Width setting format: add a number in the middle of the placeholder, the number is divided into positive and negative, +: right aligned, -: left aligned

Minimum width: the percentage sign is followed by the decimal number. If the part is not enough, you can choose to add 0.

Maximum width: Decimal number after the decimal point, the excess part will be truncated

func main() {
    ("|%s|\n", "123.456")    //|123.456|
    ("|%12s|\n", "123.456")  //|     123.456|
    ("|%-12s|\n", "123.456") //|123.456     |
    ("|%012s|\n", "123.456") //|00000123.456|
    ("|%.5s|\n", "123.456")  //|123.4|
}

This is the end of this article about in-depth understanding of formatted output in Golang. For more related Golang formatted output content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!