Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

XDM, writing C/C++ with format control characters, such as %s, %d, % C, %p, etc

When you write golang, you also have a format control, also called a placeholder, and when you write this placeholder, you have to have the corresponding data to it, you can’t mess with it

Basic Common Common placeholders

  • %s
  • %d
  • %v , %v+ , %+v
  • %T , %q

Write a demo to see what the placeholders look like

type Animal struct {
	hobby string
}

func main(a) {

	name := "xiaomotong"
	age := 19
	hh := Animal{"basketball"}

	fmt.Printf("name = %s , age = %d , hh = %v\n\n", name, age, hh)
	fmt.Printf("hh = %+v , hh= %#v\n\n", hh, hh)
	fmt.Printf("name = %T , age = %T , hh = %T\n\n", name, age, hh)
    fmt.Printf("%q".0x8989)}Copy the code

The above code executes as follows:

# go run main.goname = xiaomotong , age = 19 , hh = {basketball} hh = {hobby:basketball} , Animal{hobby:"basketball"} name = string, age = int, hh= main.Animal '覉'Copy the code

We can see from the above effect:

%q represents a character literal surrounded by single quotes, safely escaped by the Go syntax, and interested XDM can try print debugging to see the effect

$s represents a string

$d represents a decimal number

%v indicates the default format

%+v indicates that the corresponding field name will be added when the structure is printed

%#v represents the Golang language representation of the corresponding data structure

Less commonly used placeholders

  • %t
  • %b
  • %c
  • %U , %#U

Continue to write the demo to see the effect geometry:

func main(a) {
	a := true
	num := 88
	uni := 0x8989

	fmt.Printf("%t\n", a)
	fmt.Printf("%b\n", num)
	fmt.Printf("%c\n".0x8989)
	fmt.Printf("uni = %U , uni = %#U\n", uni, uni)
}
Copy the code

The above code executes as follows:

# go run main.goTrue 1011000 覉 uni = U+8989, uni = U+8989 '覉'Copy the code

We can see from the above effect:

%t represents a placeholder for a Boolean

%b represents binary data

%c represents the character represented by the corresponding Unicode code point

%U indicates that data can be converted to the Unicode format specification, which starts with +

%#U indicates that the data can be converted to unicode characters, which are pronounced jī in demo

Base and floating point use placeholders

  • %2d , %07d
  • %x , %#x
  • %f , %.3f
func main(a) {
	num := 888
	fNum := 888.99

	fmt.Printf("num = %2d , num = %07d\n", num, num)
	fmt.Printf("num = %x , num = %#x\n", num, num)
	fmt.Printf("num = %f , num = %.3f\n", fNum, fNum)
}
Copy the code

The above code executes as follows:

# go run main.goNum = 888, num = 0000888, num = 0x378, num = 888.990000, num = 888.990Copy the code

We can see from the above effect:

%2d indicates a total of two digits. If there are less than two digits, zeros are added before the digits

%07d indicates a total of 7 digits. If there are less than 7 digits, zeros are added before it

%x means hexadecimal, all lowercase

%#x represents a hexadecimal number preceded by an 0x

%f represents floating-point data, with 6 decimal places reserved by default

%.3f indicates floating point data with three decimal digits reserved

Pointer placeholders

  • %p
  • %#p
func main(a) {
	ptr := "xiaomotong"
	fmt.Printf("ptr = %p , ptr = %#p\n", &ptr, &ptr)
}
Copy the code

The above code executes as follows:

# go run main.go
ptr = 0xc42000e1e0 , ptr = c42000e1e0
Copy the code

%p represents a hexadecimal pointer address, with an 0x

%#p indicates a hexadecimal pointer address, with no 0x

Little knowledge, big challenges, placeholders, they can still be used

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Common technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~