The declaration and initialization of variables introduced by go's basic grammar

1. Common basic data types

uint8: unsigned 8-bit integer, value range: 0-255

uint16: unsigned 16-bit integer, value range: 0-65535

uint32: unsigned 32-bit integer, value range: 0-4294967295

uint64: unsigned 64-bit integer, value range: 0-18446744073709551615

uint: 32 or 64 bits

int8: signed 8-bit integer, value range: -128-127

int16: signed 16-bit integer, value range: -32768-32767

int32: signed 32-bit integer, value range: -2147483648-2147483647

int64: signed 64-bit integer, value range: -9223372036854775808-9223372036854775807

int: same size as uint

byte: like uint8

rune: similar to int32

float32: 32-bit floating point type

float64: 64-bit floating point type

string: string

bool: Boolean type, value true or false

2. Variables

Go language variable names are composed of letters, numbers, and underscores. The first character cannot be a number, and keywords cannot be used as variable names.

25 keywords in Go language:

3. Variable declaration and initialization

In HelloGo, we have declared many variables, the style is as follows:

var input string

In the above code, a string type variable named input is declared. When Golang declares variables, it will automatically initialize the memory area corresponding to the variable, and each variable will be initialized to the default value of its type. The variable declaration style is as follows:

var name T

Some common variable declaration styles are as follows:

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

var (
	f int
	g string
)
// Declare multiple sets of variables at the same time

In Golang, every declared variable must be used, otherwise it will fail to compile.

After declaring the variable, we also need to initialize the blank memory area of ​​the variable, that is, assign a value. Consistent with other languages, it is initialized with the = assignment symbol, as in the following example:

var a int = 100

In the above code, a variable a of type int is declared and assigned a value of 100. The style of variable initialization is:

var name T = expression

Of course, you can use the type derivation syntax sugar feature provided by Golang to simplify it into the following style:

var a = 100
b := "Hello"

After omitting the type attribute, the compiler tries to deduce the type of the variable from the expression on the right of the equal sign. Note that when using := short variable declaration initialization, at least one variable in the lvalue must be an undefined variable, otherwise a compilation error will occur. At the same time := cannot appear in the declaration and initialization of global variables.

var a = 100
a := 100	//compile error
a, b := 100, "OK"	//No abnormality

In the above code, a := 100 will throw no new variables on left side of := error during compilation; but a, b := 100 will not.

We can try to run the code in Variable to see the type deduction results of the compiler.

// Variable.go
package main

import "fmt"

func main()  {

	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)

}

The output is as follows:

a value is 100, type is int
b value is 100, type is string
c value is 0.17, type is float64

As can be seen from the above representation results, the variables are assigned the correct variable type. It should be noted that in order to provide precision, the floating point type will be deduced as float64 by default.

Compared with the C language, in addition to the syntactic sugar feature of type deduction, Golang also provides the syntactic sugar feature of multiple assignment and anonymous variables.

In past programming languages, if we want to exchange the value of a variable, we need to use a third-party temporary variable to achieve it, as shown in the following example:

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

tmp = a
a = b
b = tmp

In Golang, we can easily achieve similar variable exchange tasks through the feature of multiple assignment, as shown below:

var a int = 1
var b int = 2

b, a = a, b

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

In Golang, the declared variable must be used, otherwise an exception will be thrown by the compiler. Golang supports functions with multiple return values ​​and the above-mentioned multiple assignments, but sometimes we don’t need to use some lvalues, we can use anonymous variables to deal with them, as shown in Anonymous.go for specific examples:

package main
import "fmt"

// Returns a person's first and last name
func getName() (string, string){
	return "king", "small two"
}

func main()  {
	surname, _ := getName()
	_, personalName := getName()

	fmt.Printf("My surname is %v and my personal name is %v", surname, personalName)
}

4. Count the number of bytes occupied by a data type

Import package import "unsafe"

Use the functions in the import package

unsafe.Sizeof(variable)

Tags: Go Back-end programming language

Posted by hhstables on Sun, 22 Jan 2023 04:32:06 +0530