What is a variable?

A variable is a name assigned to a storage location to store a particular type of value.

How do I declare variables?

There is a variety of syntax for declaring variables in GO

Declaring a single variable

Grammar:

var name type
Copy the code
  • Var: is a keyword used to declare variables
  • Name: indicates the variable name
  • Type: The variable is a type

Example:

var username string
Copy the code

Var name string Specifies a variable named username type string.

If no value is assigned to a variable, Go automatically initializes it with a zero value of the variable type. Example:

package main

import "fmt"

func main(a) {  
    var number int 
    fmt.Println("The value of the variable number is:, number)
}
Copy the code

Running results:

The value of the variable number is: 0Copy the code

Declare a variable with an initial value

Grammar:

var name type = initialvalue  
Copy the code

Example:

var number int = 25
Copy the code

Multivariable declaration

To declare multiple variables in a single statement, first we need to know about type inference in GO

What is type inference?

When a variable is declared, it has an initial value, which Go automatically uses to infer the type of the variable. This way, when declaring variables, you can delete the keyword corresponding to the type. Such as:

var number  int =12
Copy the code

The above declarative assignment statement can be written like this

var number =  12
Copy the code

Now that type inference is clear, let’s look at multivariable declarations

Grammar:

Var name1, name2 type = initialValue1, initialValue2Copy the code

Example:

Var number1, number2 int = 10, 20 // Go determines the type of the variable based on the type of the assignment var number1, number2 = 10, 20Copy the code

The above two statements are equivalent.

Of course, multivariable declarations also vary from data type to data type. Grammar:

var (  
      name1 = initialvalue1
      name2 = initialvalue2
)
Copy the code

Example:

package main

import "fmt"

func main(a) {  
    var (
        name   = "naveen"
        age    = 29
        height int
    )
    fmt.Println("my name is", name, ", age is", age, "and height is", height)
}
Copy the code

Declare a variable name of type string, age and height of type int. The data types of name and age are determined by assignment (type inference).

Short variable declaration

Go also provides a neat way to declare variables. The syntax is as follows:

Name: = the initialvalueCopy the code

Example:

package main

import "fmt"

func main(a) {  
    number := 10
    
    name,age := "Bill".20
    
    fmt.Println("number =",number)
    
    fmt.Println("name:", name, " age: ", age)
}
Copy the code

Running results:

Number = 10 Name: Age: 20Copy the code

Note 1: When declaring variables in this neat way, you must assign values to all the variables on the left. Otherwise, an error will be reported! Example:

package main

import "fmt"

func main(a) {

	name, age := "Bill"

	fmt.Println("name:", name, " age: ", age)
}
Copy the code

Running results:

# command-line-arguments
./main.go:7:12: assignment mismatch: 2 variables but 1 values
Copy the code

Note 2: When declaring a variable in this neat way, you can change the value of the old variable, but only if there is a new variable to the left of =. Otherwise, an error will be reported!

Example:

package main

import "fmt"

func main(a) {

	name, age := "Bill".20
	fmt.Println("name:", name, " age: ", age)

	age, height := 30.175

	fmt.Println(" age: ", age, " height:", height)
}
Copy the code

Running results:

Name: Li Si age: 20 age: 30 height: 175Copy the code

We declare assignment again to the variables already declared in the above code, which looks like this

package main

import "fmt"

func main(a) {

	name, age := "Bill".20
	fmt.Println("name:", name, " age: ", age)

	age, height := 30.175

	age, height := 40.185
	fmt.Println(" age: ", age, " height:", height)

}

Copy the code

Running results:

# command-line-arguments
./main.go:14:14: no new variables on left side of :=
Copy the code

Create pointer variables using the keyword new

A pointer variable, as its name suggests, is a variable that holds an address (the location in memory where data resides). Example:

package main

import "fmt"

func main(a) {
	number1 := 20
	var ptr1 = &number1
	ptr2 := new(int)

	fmt.Println("number1: ", number1)
	fmt.Println("ptr1: ", ptr1)
	fmt.Println("ptr2: ", ptr2)

	fmt.Println("Ptr2 point to the data value:", *ptr2)
}
Copy the code

Running results;

Number1:20 Ptr1:0xC00010C000 Ptr2:0xC00010C008 Data value to which the PTR2 pointer points: 0Copy the code

In the code above, the variables ptr1 and ptr2 are pointer variables. The ptr2 variable is created using the expression new (Type).

New (Type) : creates an anonymous variable of Type Type, initializes the zero value of Type Type, and returns the address of the anonymous variable.

The make function creates a reference datatype variable

Reference data types in GO: Slice, map, chan Create and initialize reference data types using make. Example:

	// Make is used in channel
	var a chan int // Declare a variable
	if a == nil {
		fmt.Println("channel a is nil, going to define it")
		a = make(chan int) // Set a channel
	}
	b := make(chan int) Declare and define a channel

	// Make is used in map
	var map1 map[string]int     // map1 is nil
	map1 = make(map[string]int) // Use the make function to initialize

	map2 := make(map[string]int)
	//map2["abc"] = 123

	fmt.Println(map1, b, a, map2)

	// Make usage in slice
	i := make([]int.5.5) // Create a slice using make

Copy the code

Welcome to pay attention to my public number “xiaodong programming”, original technical articles pushed the first time.