[30 days familiar with Go language] 4 Go variables, constants, operators

I. Introduction

insert image description here

Go series articles:

  1. The beginning of GO: Entering the world of Golang with Java in hand
  2. 2 Go development environment setup, Hello World program running
  3. 3 Go programming specification and API package

Go column delivery link: https://blog.csdn.net/saintmm/category_12326997.html

2. Variables

A variable is equivalent to the identifier of a data storage space in memory.

1. Basic use of variables

Variables are used in three steps: declaration, assignment, and use.

  • Variable declarations take the form var 变量名 数据类型of .
package main

import "fmt"

func main() {
    
    
	// 1. 变量的声明
	var age int
	// 2. 变量的赋值
	age = 18
	// 3. 变量的使用
	fmt.Println("age = ", age)
}

Variable declaration and assignment can be combined into one sentence:

var age int = 19

If the variable is defined repeatedly, an error will be reported in GoLand:
insert image description here

When the variable is assigned, the data does not match the type, and the error is as follows:

insert image description here

2. Multiple ways of using variables

1) Global variables

A global variable is a variable defined outside a function that can be used by multiple functions.

package main

import "fmt"

var globalV1 = 100
var globalV2 = 1.11

func main() {
    
    
	variable1()
	//variable2()
}

func variable1() {
    
    
	fmt.Println(globalV1)
	fmt.Println(globalV2)
}

func variable2() {
    
    
	fmt.Println(globalV1)
	fmt.Println(globalV2)
}

If there are multiple global variables, it is too troublesome to use var to declare each time; so Go supports declaring multiple variables of different types at once;

var (
	globalV1= 3.33
	globalV2= "Hello"
)

2) Local variables

Local variables are variables declared in function {};

1> Specify the type of variable and assign it:

var v1 int = 18
fmt.Println(v1)

2> Specify the type of the variable, but do not assign a value, use the default value:

var v2 int
fmt.Println(v2)

3> If the type of the variable is not written, the program will automatically judge the type of the variable according to the value after =the number (automatic type inference):

var v3 = "saint"
fmt.Println(v3)

4> You can use :=instead of var to declare and assign variables:

v4 := "bob"
fmt.Println(v4)

5> Declare multiple variables of the same type:

var n1, n2, n3 int
fmt.Println(n1)
fmt.Println(n2)
fmt.Println(n3)

6> Declare multiple variables of different types:

var n4, name, n5 = 9, "jack", 7.8
fmt.Println(n4)
fmt.Println(name)
fmt.Println(n5)

// 省略var的方式
n6, sex := 6.9, "man"
fmt.Println(n6)
fmt.Println(sex)

3) Discard assignment

_Receive values ​​using identifiers , assignments can be discarded;

Discard assignment must be used in scenarios where two or more values ​​are taken at a time and one of the values ​​is not desired.

var _,_,num = 1,2,3
fmt.Println("num = ",num)

The code snippet will only accept the value 3 and assign it to the num variable.

3. Variable comparison between Go and Java

  1. Variable declaration and assignment methods are different; Java is 变量类型 变量名 = 变量值, Go isvar 变量名 变量类型 = 变量值

    // Java 变量类型  变量名 = 变量值
    String name = "li_ming";
    
    // Go var  变量名  变量类型 = 变量值
    var name string = "li_ming"
    
  2. Java's variable declaration needs to specify the data type of the variable; Go's variable declaration does not need to specify the data type of the variable (the type is automatically inferred)

    // go类型自动推断
    var v3 = "saint"
    
  3. Variable assignments inside and outside Java methods can only be used =; Go's variable declaration assignments can be abbreviated ( ) inside the function 变量名 := 变量值, and can only be used outside the functionvar 变量名 = 变量值

    // 函数内部可以直接使用 【 变量名 := 变量值 】 赋值,函数外不可以
    name := "saint"
    
  4. Go supports multi-variable assignment at the same time, and lost assignment (using an identifier _to receive a value can discard the assignment, but it must be used when two or more values ​​are taken at a time and one of the values ​​​​is not wanted)

    // 多变量同时赋值
    var name,age = "saint",18
    
    // 丢弃赋值,示例中:把 1和2丢弃 只取3
    var _,_,num = 1,2,3
    fmt.Println("num = ",num)
    

3. Constants

The only difference between Go's constant and variable declarations is: the use of constant declarations constand the use of variable declarations var. If you have written the front end for a while, you will find that Go's constant and variable declarations are similar to the front end.

//const  常量名  常量类型 = 常量值   显示推断类型
const name string = "const_sanint"
//隐式推断类型
const name2 ="const_sanint2"

1. Comparison of constants between Go and Java

There is an essential difference between constants in Go and constants in Java:

  • A constant in Go refers to an amount that can be determined during compilation; a constant in Java refers to an JVM跑起来后赋值的amount that cannot be modified after being assigned a value ( ).
  1. Constants are declared differently; Java is final修饰符 常量类型 常量名 = 常量值, Go isconst 常量名 常量类型 = 常量值

    // Java final修饰符   常量类型   常量名 =  常量值;
    public static final String TAG = "A";
    
    // go const  常量名  常量类型 = 常量值   显示推断类型
    const name string = "const_li_ming"
    // go 隐式推断类型
    const name2 ="const_xiao_hong"
    

3. Operators

An operator is a special symbol used to represent data operations, assignments, and comparisons. Commonly used operators in Go are as follows:

insert image description here

++Here is almost the same as the usage of the Java language, here are some explanations for the , --and other operators in the arithmetic operators .

1. Arithmetic operators ++, –

++ Self-increment 1 operation, – self-decrement, minus 1 operation.

var a int = 10
a++
fmt.Println(a)
a--
fmt.Println(a)

Differences from Java:

  • In Go, ++, --the operation is very simple, it can only be used alone, and cannot participate in the operation, for example: , age = ++age + 10this kind of complex operation is not allowed.
  • In Go, ++, --can only be written after the variable, and cannot be written in front of the variable ( --a ++athese are all wrong ways of writing)

2. Operators &, *

In Go &and *have special purpose:

  • &: returns the storage address of the variable
  • *: Get the value corresponding to the pointer variable
package main

import "fmt"

func main() {
    
    
	var age int = 18
	fmt.Println("age对应的存储空间内存地址为:",&age)

	// 指针变量 ptr,指向age在存储空间的内存地址
	var ptr *int = &age
	fmt.Println(ptr)
	fmt.Println("ptr这个指针指向的具体数值为:",*ptr)
}

See the following article for the content of the pointer.

Four. Summary

For the declaration and assignment of variables/constants, compared with Java, Go has some unique properties:

  1. Support for declaring multiple variables/constants of different types at the same time;
  2. Support type automatic inference
  3. Discard assignment is supported.

In addition, for constants, Java and Go have different perceptions:

  • Java believes that after the JVM runs, the constant value is assigned once, and it is not allowed to change later.
  • Go's constants are quantities that can be determined at compile time and are also not allowed to be modified.

Go's arithmetic operators cannot be used in complex operations like Java, and can only be used in the simplest way ++.--

And Go's &and *have special purpose.

Guess you like

Origin blog.csdn.net/Saintmm/article/details/130839198