xdm, when writing C/C++ languages, there are format controllers, such as %s, %d, %c, %p, etc.
When writing golang, there is also a corresponding format control character, also called a placeholder. When writing this placeholder, you need to have corresponding data to correspond to it, so you can't do it.
Basic common placeholders
- %s
- %d
- %v , %v+ , %+v
- %T , %q
Write a demo to see the effect of the placeholder above, what exactly is it like
type Animal struct { hobby string } func main() { name := "xiaomotong" age := 19 hh := Animal{"basketball"} ("name = %s , age = %d , hh = %v\n\n", name, age, hh) ("hh = %+v , hh= %#v\n\n", hh, hh) ("name = %T , age = %T , hh = %T\n\n", name, age, hh) ("%q", 0x8989) }
The above code execution effect is as follows:
# go run name = xiaomotong , age = 19 , hh = {basketball} hh = {hobby:basketball} , hh= {hobby:"basketball"} name = string , age = int , hh = 'Share'
From the above effects we can see:
- %q represents the character literal value surrounded by single quotes, which is safely escaped by Go syntax. If you are interested in xdm, you can try printing and debugging to see the effect.
- $s represents a string
- $d represents a decimal number
- %v means the default format
- %+v means that when printing the structure, the corresponding field name will be added.
- %#v represents the representation of the golang language of the corresponding data structure
Less used placeholders
- %t
- %b
- %c
- %U , %#U
Continue to write a demo to check the effect geometry:
func main() { a := true num := 88 uni := 0x8989 ("%t\n", a) ("%b\n", num) ("%c\n", 0x8989) ("uni = %U , uni = %#U\n", uni, uni) }
The above code execution effect is as follows:
# go run true 1011000 Watch uni = U+8989 , uni = U+8989 'Share'
From the above effects we can see:
- %t means a placeholder for a boolean
- %b represents binary data
- %c represents the characters represented by the corresponding Unicode code point
- %U means that data can be converted into Unicode format specifications, that is, starting with +
- %#U means that data can be converted into characters corresponding to unicode, and the text in demo is pronounced jī
Use placeholders for both binary and floating point
- %2d , %07d
- %x , %#x
- %f , %.3f
func main() { num := 888 fNum := 888.99 ("num = %2d , num = %07d\n", num, num) ("num = %x , num = %#x\n", num, num) ("num = %f , num = %.3f\n", fNum, fNum) }
The above code execution effect is as follows:
# go run num = 888 , num = 0000888 num = 378 , num = 0x378 num = 888.990000 , num = 888.990
From the above effects we can see:
%2d means a total of 2 digits. If there are less than 2 digits, the zero will be added before it.
%07d means a total of 7 digits. If the number is less than 7 digits, the zero will be added before it.
%x means hexadecimal, all in lowercase
%#x means hexadecimal, with 0x in front
%f represents floating point type data, and the default is to retain 6 decimal places.
%.3f represents floating point type data, retaining 3 decimal places
Pointer placeholder
- %p
- %#p
func main() { ptr := "xiaomotong" ("ptr = %p , ptr = %#p\n", &ptr, &ptr) }
The above code execution effect is as follows:
# go run ptr = 0xc42000e1e0 , ptr = c42000e1e0
%p means the hexadecimal pointer address, with 0x
%#p means the hexadecimal pointer address, there will be no 0x
This is the end of this article about learning about golang placeholders. For more related golang placeholders, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!