This article brings you relevant knowledge about golang. It mainly talks about placeholders in golang. Friends who are interested should take a look at it. I hope it will be helpful to everyone.
xdm, when writing C/C language, there are format control characters, such as %s, %d, %c, %p, etc.
When writing golang, there is also a corresponding format control character, also called a placeholder. To write this placeholder, you need to have corresponding data to correspond to it, and you can't mess around.
Write a demo to see the effect of the above placeholder and what it looks like
type Animal struct { hobby string } func main() { 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) }
Above The code execution effect is as follows:
# go run main.go name = xiaomotong , age = 19 , hh = {basketball} hh = {hobby:basketball} , hh= main.Animal{hobby:"basketball"} name = string , age = int , hh = main.Animal '覉'
We can see from the above effect:
%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 represents the default format
% v indicates that when printing the structure, the corresponding field names will be added
##%#v indicates the representation of the golang language of the corresponding data structure
Less used placeholdersfunc main() { 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) }
The execution effect of the above code is as follows:
# go run main.go true 1011000 覉 uni = U+8989 , uni = U+8989 '覉'
%b represents binary data
%c represents the character represented by the corresponding Unicode code point
%U represents that the data can be converted into Unicode Format specification, that is, the %#U at the beginning means that the data can be converted into unicode corresponding characters. The text in the demo readsjī
func main() { 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) }
The execution effect of the above code is as follows:
# go run main.go num = 888 , num = 0000888 num = 378 , num = 0x378 num = 888.990000 , num = 888.990
d means a total of 7 digits, if there are less than 7 digits, add zeros in front
%x means Hexadecimal, all lowercase %#x represents hexadecimal, preceded by 0x%f to represent floating point type data, the default is to keep 6 Number of decimal places
%.3f represents floating point type data, retaining 3 decimal placesfunc main() { ptr := "xiaomotong" fmt.Printf("ptr = %p , ptr = %#p\n", &ptr, &ptr) }
The above code execution effect is as follows:
# go run main.go ptr = 0xc42000e1e0 , ptr = c42000e1e0
[Related recommendations: Go Video Tutorial】
The above is the detailed content of One article to thoroughly understand golang placeholders. For more information, please follow other related articles on the PHP Chinese website!