Home > Backend Development > Golang > One article to thoroughly understand golang placeholders

One article to thoroughly understand golang placeholders

藏色散人
Release: 2023-03-16 15:47:02
forward
2693 people have browsed it

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.

One article to thoroughly understand golang placeholders

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.

Basically common placeholders

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

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)
}
Copy after login

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

'覉'
Copy after login

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 placeholders

  • %t
  • %b
  • %c
  • %U , %#U
Continue writing the demo to check the effect geometry:

func 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)
}
Copy after login

The execution effect of the above code is as follows:

# go run main.go
true
1011000
覉
uni = U+8989 , uni = U+8989 '覉'
Copy after login

We can see from the above effect:

%t represents the Boolean placeholder

%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 reads

base and floating point Use placeholders

  • - , d
  • %x , %#x
  • %f , %. 3f
  • 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)
    }
    Copy after login

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
Copy after login

We can see from the above effect:

- means a total of 2 digits, if there are less than 2 digits, add zeros in front

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 places

Pointer placeholder

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

The above code execution effect is as follows:

# go run main.go
ptr = 0xc42000e1e0 , ptr = c42000e1e0
Copy after login
%p means The hexadecimal pointer address will contain 0x

%#p which means the hexadecimal pointer address will not contain 0x

[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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template