banner
音小董

哩哔哩哔

这世上的热闹出自孤单

Chapter 1: Variables

Introduction to Go Language#

SDK installation tutorial:https://www.bilibili.com/video/av954201380?p=4

#

Official SDK download address:https://golang.google.cn/dl/

Command to enable go modules:

go env -w GO111MODULE=on

Command to set up a domestic proxy:

go env -w GOPROXY=https://proxy.golang.com.cn,https://goproxy.cn,direct

Download and activation tutorial for Goland:https://www.jiweichengzhu.com/ide/code

Golang standard library API documentation:https://studygolang.com/pkgdoc

Basic Variables#

Variables#

What is a variable?#

==Answer: A variable represents a data storage space in memory.==

Steps to use a variable#

==1. Declaration==

==2. Assignment==

==3. Usage==
Example 1

package main

import "fmt"

func main() {
	//1. Declaration
	var age int
	//2. Assignment
	age = 18
	//3. Usage
	fmt.Println("age = ", age)
}

Output

18

Example 2

package main

import "fmt"

func main() {
	var name string
	name = "Jay Chou"
	fmt.Println("My name is:", name)
}

Output

My name is: Jay Chou

4 ways to use variables#

package main

import (
	"fmt"
	"reflect"
)

func main() {
	//First way
	var num int = 6
	fmt.Println(num)
	//Second way (Specify variable type, but if not assigned, it will automatically use the default value. For example, the default value of int is 0)
	var num2 int
	fmt.Println(num2)
	//Third way (Do not specify variable type, it will automatically determine the variable type based on the value after the equal sign)
	var num3 = 0.5
	fmt.Println(reflect.TypeOf(num3))
	//Fourth way, based on the third way, omit var, = must be rewritten as :=
	sex := "Male"
	fmt.Println(sex)
}

Output

6
0      
float64
Male   

Support for declaring multiple variables at once (Multiple variable declaration)#

package main

import "fmt"

func main() {
	var b1, b2, b3 int = 1, 2, 3
	fmt.Println(b1)
	fmt.Println(b2)
	fmt.Println(b3)
	fmt.Println("_________________Separator_____________________")
	var a1, a2, a3 int
	fmt.Println(a1)
	fmt.Println(a2)
	fmt.Println(a3)
	fmt.Println("_________________Separator_____________________")
	var c1, c2, c3 = 5, 0.5, "Five"
	fmt.Println(c1)
	fmt.Println(c2)
	fmt.Println(c3)
	fmt.Println("_________________Separator_____________________")
	d1, d2, d3 := 7, 8, 9
	fmt.Println(d1)
	fmt.Println(d2)
	fmt.Println(d3)
}

Output

1
2                                           
3                                           
_________________Separator_____________________
0                                           
0                                           
0                                           
_________________Separator_____________________
5                                           
0.5                                         
Five                                        
_________________Separator_____________________
7                                           
8                                           
9  

Local variables and global variables#

Local variables: Variables defined within a function (i.e., variables within function parentheses, all variables mentioned above are local variables)

Global variables: Variables defined outside a function

Example of global variables

package main

import "fmt"

var n = 666
var m = 888

// Or declare all at once, shorthand:
var (
	x = 666
	y = 888
)

func main() {
	fmt.Println(n)
	fmt.Println(m)
	fmt.Println(x)
	fmt.Println(y)
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.