Starting from Hello World, we will teach you step by step how to write your first Go program.

1. Development editor

Go uses UTF-8 encoded text files to store source code. In theory, any text editor can be used for Go language development. VS Code and Goland are recommended here.

VS Code is an open source editor from Microsoft, while Goland is a paid IDE produced by jetbrains. GoLand charges a fee when developing tools. It has a free trial period of one month. After expiration, you can try to uninstall and reinstall it. Currently, there is no particularly good cracking tutorial for this tool. We use VS Code and plug-ins here as Go language development tools.

2. Install the Go plug-in in VS Code

The installation and Chinese localization of VsCode will not be introduced in detail. Friends in need can refer to other articles. We directly open VS Code and search for GO in the extension, as shown below:

Insert image description here
Click Install and then restart VsCode

3. The first Go program

3.1、Hello World

1. First create a new folder, for example: go_demo is used to store the Go projects during our learning process, then create our first Go program HelloWorld folder in the go_demo directory, and finally open this folder through VSCode, as shown below

Insert image description here
Insert image description here
2. When using go module mode to create a new project, we need to initialize the project through the go mod init project name command.

# 项目初始化
go mod init HelloWorld

This command will generate the go.mod file in the project root directory. As shown below

Insert image description here
3. Create a new Go file. VSCode will activate the Go plug-in. Just install all the tools prompted, as shown below:

Insert image description here
If a prompt pops up in the lower right corner of VSCode asking you to install the plug-in, be sure to click install all to install it. Waiting for the installation to be completed, our VSCode can be used normally.

4. Write code

// 声明 main 包,表明当前是一个可执行程序
package main

// 导入内置 fmt 包
import "fmt"

// main函数,是程序执行的入口
func main(){
    
    
	// 在终端打印 Hello World
	fmt.Println("Hello World")
}

Then continue to manually execute the go file in the terminal and enter the command go run file name, as shown below:

Insert image description here

5. Compile and package

Compile the go file manually in the terminal and enter the command go build file name

# 启动运行命令
go run csdn.go

# 编译打包命令
go build csdn.go

# 可以以使用 -o 参数来指定编译后得到的可执行文件的名字。
go build -o csdn_vip

6. One note per day

6.1. Automatic derivation of types

The type is automatically deduced. When declaring an assignment variable, var and variable type are not required. The type is determined by the assigned value.

Basic syntax format: "Variable name:= value".

Multiple assignment syntax format: "Variable name 1, variable name 2, variable name 3 := value 1, value 2, value 3"

// 声明 main 包,表明当前是一个可执行程序
package main

// 导入内置 fmt 包
import "fmt"

// main函数,是程序执行的入口
func main() {
    
    

	// 第一种写法
	var a int // 声明变量
	a = 1     // 变量赋值

	// 第二种写法
	var b int = 1 // 变量的初始化,声明变量并赋值

	// 第三种写法,Go 语言常用写法
	c := 1 // 自动推导类型,必须初始化,通过初始化的值确定类型

	// %v	值的默认格式表示
	// %+v	类似%v,但输出结构体时会添加字段名
	// %#v	值的Go语法表示
	// %T	值的类型的Go语法表示
	// %%	百分号

	// 在终端打印 a
	fmt.Printf("a 的数据类型是:%T,值:%v\n", a, a)
	// 在终端打印 b
	fmt.Printf("b 的数据类型是:%T,值:%v\n", b, b)
	// 在终端打印 c
	fmt.Printf("c 的数据类型是:%T,值:%v\n", c, c)
}

Code running results:

a 的数据类型是:int,值:1
b 的数据类型是:int,值:1
c 的数据类型是:int,值:1

6.2. Anonymous variables

During coding, you may encounter variables, types, or methods without names. Although this is not required, sometimes doing so can greatly enhance the flexibility of your code. These variables are collectively called anonymous variables.

Anonymous variables are characterized by an underscore " ", which itself is a special identifier, called a blank identifier. It can be used in variable declarations or assignments like other identifiers (any type can be assigned to it), but any values ​​assigned to this identifier will be discarded, so these values ​​cannot be used in subsequent code, nor This identifier cannot be used as a variable to perform assignments or operations on other variables. When using anonymous variables, you only need to replace them with underscores where the variable is declared. For example:

// 声明 main 包,表明当前是一个可执行程序
package main

import "fmt"

// 导入内置 fmt 包

// main函数,是程序执行的入口
func main() {
    
    

	// 第一种方法
	a, b := GetData()
	// 在终端打印 a 和 b
	fmt.Printf("a 的数据类型是:%T,值:%v;b 的数据类型是:%T,值:%v\n", a, a, b, b)

	// 第二种写法,只需要第一个返回值,把第二个返回值定义为匿名变量
	c, _ := GetData()

	// 第二种写法,只需要第二个返回值,把第一个返回值定义为匿名变量
	_, d := GetData()
	// 在终端打印 c 和 d
	fmt.Printf("c 的数据类型是:%T,值:%v;d 的数据类型是:%T,值:%v\n", c, c, d, d)
}

// 定义一个函数,第一个返回整型值,第二个返回字符
func GetData() (int, string) {
    
    
	return 2022, "Hello World"
}

Code running results:

a 的数据类型是:int,值:2022;b 的数据类型是:string,值:Hello World
c 的数据类型是:int,值:2022;d 的数据类型是:string,值:Hello World

This tutorial ends here, friends can happily start their programming journey.

Guess you like

Origin blog.csdn.net/u011374856/article/details/127905604