SoFunction
Updated on 2025-03-02

Example of implementation of formatted placeholders in Go language

In Go,fmtThe package provides a large number of formatted placeholders for formatting and outputting different types of data. Selecting the right placeholder can ensure that the output content is formatted correctly and is clear and easy to understand.

Common placeholders:

Basic Type

  • %v: Output by default format of value. Suitable for any type.
  • %+v:exist%vBased on the structure, the field name will be output additionally.
  • %#v: Output the value represented by Go syntax. The structure will output detailed field information and types.
  • %T: The type of output value.
  • %%: Literal percent sign (%self).

Integer Type

  • %d: Decimal integer.
  • %b: Binary format.
  • %o: Octal format.
  • %x: Hexadecimal format (lowercase letters).
  • %X: Hexadecimal format (capsular letters).
  • %c: Characters of the corresponding Unicode code point.

Floating point numbers and plural numbers

  • %f: Standard floating point representation (decimal part).
  • %e: Scientific notation method (lowercase e).
  • %E: Scientific notation (caps E).
  • %g: Choose according to the specific situation%for%eexpress.

Strings and bytes

  • %s: String.
  • %q: String with double quotes, safe escape.
  • %x: represents a string or byte slice in hexadecimal.
  • %X: Represents a string or byte slice in capital hexadecimal.

Boolean type

  • %t: Boolean value (trueorfalse)。

pointer

  • %p: Hexadecimal representation of pointer.

How to choose a placeholder:

When selecting a placeholder, you first need to select the appropriate formatting symbol according to the data type. The specific situation is as follows:

Output general data type

use%vIt is a universal and default choice. If you are not sure about the type,%vAll types of values ​​can be output. For example:

("Result: %v", result)

Output structure

If you want to view the detailed information of the structure, you can use%+vOutput field names and values, or use%#vOutput the Go language format value. For example:

("Struct data: %+v", myStruct)
("Go syntax: %#v", myStruct)

Output string

General use%sOutput string; if you need to output a safe string with double quotes and escape characters, use%q. For example:

("User input: %s", input)
("Escaped string: %q", input)

Output integer

use%dOutput decimal integers. If they need to be represented in binary or hexadecimal, they can be used separately.%band%x. For example:

("User age: %d", age)
("Memory address in hex: %x", pointer)

Output floating point number

General use%fOutput a floating point number, the output has a decimal part; if scientific notation is required, use%eor%E. For example:

("Price: %f", price)
("Price in scientific notation: %e", price)

Output pointer

If you need to output a pointer value, use%pIts hexadecimal address will be displayed. For example:

("Pointer address: %p", &myVar)

Actual case:

Suppose we have the following data:

taskID := 12345
price := 1234.567
name := "Alice"
completed := true
myStruct := struct {
	ID   int
	Name string
}{
	ID:   1,
	Name: "TestTask",
}

Use placeholder output:

("Task ID: %d", taskID)                // Output decimal integers("Price: %f", price)                   // Output floating point number("User: %s", name)                     // Output string("Task completed: %t", completed)      // Output boolean value("Task info: %+v", myStruct)           // Output structure with field name and value

Placeholder list:

Placeholder illustrate Example
%v Default format ("%v", 42)
%+v Structure fields and values ("%+v", obj)
%#v Go syntax represents value ("%#v", obj)
%T Type of output value ("%T", obj)
%d Decimal integers ("%d", 123)
%b Binary integers ("%b", 7)
%x Hexadecimal lowercase ("%x", 255)
%f Floating point number ("%f", 3.14)
%t Boolean value ("%t", true)
%p Pointer address ("%p", ptr)
%s String ("%s", "abc")
%q String with double quotes ("%q", "abc")

By selecting the correct placeholder according to the data type, the formatting and readability of the output information can be ensured.

This is the article about the implementation example of formatted placeholders in Go language. For more related Go formatted placeholders, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!