Go language basic variable declaration and initialization example explanation

Time:2022-1-6
catalogue
  • 1、 Overview
  • 2、 Declare variable
  • 3、 Compiler derivation type format [must be assigned]
  • 4、 Short variable declaration and initialization
  • 5、 Anonymous variables – variables without names
  • 6、 Attention
  • 7、 Case

1、 Overview

The function of variables is to store user data

2、 Declare variable

Each variable of go language has its own type, which must be declared before it can be used

Declaration format of variable:

Var < variable name > [variable type]

?
1
2
3
4
5
6
7
Var a int // declare an integer variable that can hold integer values
Var B string // declare a variable of string type
Var C float32 // declare a variable of 32-bit floating-point slice type, which represents a data structure composed of multiple floating-point types
Var D func() bool // declare a function variable whose return value is Boolean. This form is generally used for callback functions
Var e struct {// declare a variable of structure type
     x int
 }

a. Standard format

It starts with the keyword VaR, and the post variable type. There is no semicolon at the end of the line

Var variable name variable type

b. Batch format

?
1
2
3
4
5
6
7
8
9
var (
    a int
    b string
    c float32
    d func() bool
    e struct {
        x int
    }
)

3、 Compiler derivation type format [must be assigned]

After omitting the type, the compiler attempts to derive the type of the variable from the expression to the right of the equal sign

?
1
var hp = 100

4、 Short variable declaration and initialization

Omit type and VaR, and change = to =:
Since ‘: = “is used instead of the assigned’ = ‘, the lvalue variable of the derivation declaration writing method must be a variable that has not been defined.
If defined, a compilation error will occur

?
1
2
xp := 10
fp,ap=20,30

5、 Anonymous variables – variables without names

When using multiple assignments, anonymous variables can be used if you do not need to receive variables in lvalues.
The expression of anonymous variable is a “” Draw a line below. When using an anonymous variable, you only need to replace it with a line below where the variable is declared

?
1
a,_=10,20

6、 Attention

  • When the compiler deduces a type, [must assign a value]
  • Since ‘: = “is used instead of the assigned’ = ‘, the lvalue variable of the derivation declaration writing method must be a variable that has not been defined. [if defined, a compilation error will occur]
  • It’s better to have more than two variables to use anonymous variables [otherwise it will lose its meaning]

7、 Case

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import "fmt"
import "net"
func main() {
    /*1、 Statement*/
    Var a int // declare an integer variable that can hold integer values
    Var B string // declare a variable of string type
    Var C float32 // declare a variable of 32-bit floating-point slice type, which represents a data structure composed of multiple floating-point types
    Var D func() bool // declare a function variable whose return value is Boolean. This form is generally used for callback functions
    Var e struct {// declare a variable of structure type
        x int
    }
    Var f bool // declare a boolean variable
    fmt. Printf ("a type:% T, value:% v \ n", a, a) // a type: int, value: 0
    fmt. Printf ("Btype:% T, value:% Q \ n", B, b) // Btype: string, value: ""
    fmt. Printf ("C type:% T, value:% v \ n", C, c) // C type: float32, value: 0
    fmt. Printf ("D type:% T, value:% v \ n", D, d) // D type: func() bool, value: < nil >
    fmt. Printf ("E type:% T, value:% v \ n", e, e) // E type: struct {x int}, value: {0}
    fmt. Printf ("F type:% T, value:% v \ n", F, f) // F type: bool, value: false
    /*2、 Batch declaration*/
    //  var (
    //      a int
    //      b string
    //      c float32
    //      d func() bool
    //      e struct {
    //          x int
    //      }
    //  )
 
    //3、 The compiler deduces the format of the type [must assign a value, which is to deduce the type at compile time]
    var hp = 100
    fmt.Println(hp)
    fp,ap:=20,30
    fmt.Println(fp,ap)
    //4、 Short variable declaration and initialization
    //HP: = 10 // error no new variables on left side of: = no new variables appear on the left side of ": =, which means that the variables on the left side of": = "have changed
    //Multiple short variable declarations [the compiler will not report err repetition]
    conn1, err := net.Dial("tcp", "127.0.0.1:8080")
    conn2, err := net.Dial("tcp", "127.0.0.1:8080")
    fmt.Println(conn1,err)
    fmt.Println(conn2,err) 
    //5、 Anonymous variables - variables without names
    a,_=10,20
}

The above is the details of the declaration and initialization examples of go language basic variables. For more information about go language basic variables, please pay attention to other related articles of developeppaper!

Recommended Today

Wanandroid open source project

Quickly create an Android client with good experience. The original version is Java, and the current project is a mixed version with clear hierarchy and clear annotations. The project stability and UI have been greatly improved compared with the initial version. If you still feel good, just order a star (the project is continuously upgrading) […]