This is the third day of my participation in Gwen Challenge

These reviews

Previous articles have introduced Go related features and introduction cases. For a programming language, variables and syntax are the first things we need to master. Golang has a lot of syntactically similar support to C, but is more minimalist than C. If you have some background in C, you can read with less effort. Even if you don’t, Golang’s syntax is very easy to understand.

This series of articles will introduce the basic syntax of the Go language, starting with variable declaration and initialization.

Declaration and initialization of variables

In HelloGo, we have declared a number of variables, like this:

var input string
Copy the code

The above code declares a string variable named input. Golang automatically initializes the memory region corresponding to the variable when declaring it. Each variable is initialized to its default value. The variable declaration style looks like this:

var name T
Copy the code

Some common variables are declared as follows:

var a int	Declare a variable of type int
var b string	// Declare a string variable
var c []float	// Declare a float slice
var d struct{	// Declare an anonymous structure with a field of type int
	x int
}
var e func(a) bool// Declare a function variable

var (
	f int
	g string
)
// Declare multiple groups of variables simultaneously
Copy the code

In Golang, every declared variable must be used or it will compile and fail.

After declaring a variable, we also need to initialize the empty memory area of the variable, which is called assignment. As in other languages, this is initialized with the = assignment symbol, as in the following example:

var a int = 100
Copy the code

In the above code, we declare a variable of type int a and assign it the value 100. The style of variable initialization is:

varName T = expressionCopy the code

Of course, you can take advantage of the type derivation syntactic sugar features provided by Golang and reduce them to the following styles:

var a = 100
b := "Hello"
Copy the code

After omiting the type attribute, the compiler attempts to deduce the type of the variable from the expression to the right of the equal sign. Note that when initializing with := short variable declarations, at least one of the variables in the lvalue must be undefined, otherwise a compilation error will occur. Also := cannot appear in declarations and initializations of global variables.

var a = 100
a := 100	// Compile error
a, b := 100."OK"	/ / there is no exception

Copy the code

A := 100 will throw no new variables on left side of :=. And a, b := 100 won’t.

We can try running the code in Variable to see the compiler’s type derivation.

// Variable.go
package main

import "fmt"

func main(a)  {

	var a int = 100
	var b = "100"
	c := 0.17

	fmt.Printf("a value is %v, type is %T\n", a, a)
	fmt.Printf("b value is %v, type is %T\n", b, b)
	fmt.Printf("c value is %v, type is %T\n", c, c)

}

Copy the code

The following output is displayed:

A value is 100, type is int B value is 100, type is String C value is 0.17, Type is float64Copy the code

As you can see from the above representation, the variables are assigned the correct variable type. Note that the floating-point type is derived by default to float64 for accuracy.

In addition to the syntactic sugar features of type derivation, Golang offers syntactic sugar features of multiple assignments and anonymous variables compared to C.

In past programming languages, if we wanted to exchange values of variables, we would need to use a third-party temporary variable to do so, as shown in the following example:

var a int = 1
var b int = 2
var tmp int

tmp = a
a = b
b = tmp
Copy the code

In Golang, we can easily implement a similar variable exchange task with the multi-assignment feature, as follows:

var a int = 1
var b int = 2

b, a = a, b
Copy the code

In multiple assignment, the lvalue and rvalue of a variable are assigned from left to right.

In Golang, declared variables must be used or the compiler will throw an exception. Golang supports functions with multiple return values and multiple assignments, but sometimes we don’t need to use certain lvalues and can use Anonymous variables, as shown in anonymise.go:

Func getName() (string, string){return "wang "," small two "} func main() {surname, _ := getName() _, personalName := getName() fmt.Printf("My surname is %v and my personal name is %v", surname, personalName) }Copy the code

By substituting _ for variable names in place of unwanted variable declarations, we can ignore some unwanted lvalues. Anonymous variables take up no namespace and no memory is allocated. Anonymous variables are not unusable from one another because of multiple declarations.

summary

Variables generally refer to the abstract concept of stored data in the running process of the program, its value is allowed to change; The opposite is a constant, whose value is not allowed to change while the program is running. In the following articles, we will introduce the native data types of the Go language.