Go_基础语法
Work before Hello World
-
Open a command prompt and cd to home directory.
-
Create a hello directory for the first Go source code.
1
2mkdir hello
cd hello -
Enable dependency tracking for your code.
When your code imports packages contained in other modules, you manage those dependencies through your code’s own module. That module is defined by a
go.mod
file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository.To enable dependency tracking for your code by creating a go.mod file, run the
go mod init
command, giving it the name of the module your code will be in. The name is the module’s module path.
In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might begithub.com/mymodule
. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies.
But for the purposes of just a tutorial, just useexample/hello
.1
2$ go mod init example/hello
go: creating new go.mod: module example/hello -
In your text editor, create a file hello.go in which to write your code.
-
Paste the following code into your hello.go file and save the file.
1
2
3
4
5
6
7package main
import "fmt"
func main(){
fmt.Println("Hello, World!")
}This is your Go code. In this code, you:
- Declare a
main
package (a package is a way to group functions, and it’s made up of all the files in the same directory). - Import the popular
fmt
package, which contains functions for formatting text, including printing to the console. This package is one of the standard library packages you got when you installed Go. - Implement a
main
function to print a message to the console. Amain
function executes by default when you run themain
package.
- Declare a
-
Run the code to see the greeting.
1
2$ go run .
Hello, World!
Key Points
- When your code imports packages contained in other modules, you manage those dependencies through your code’s own module. That module is defined by a
go.mod
file that tracks the modules that provide those packages. That go.mod file stays with your code, including in your source code repository. - To enable dependency tracking for your code by creating a go.mod file, run the
go mod init
command, giving it the name of the module your code will be in. The name is the module’s module path. - In actual development, the module path will typically be the repository location where your source code will be kept. For example, the module path might be
github.com/mymodule
. If you plan to publish your module for others to use, the module path must be a location from which Go tools can download your module. For more about naming a module with a module path, see Managing dependencies. - Package is a way to group functions, and it’s made up of all the files in the same directory.
- A
main
function executes by default when you run themain
package. - go mod init command:
go mod init example/hello
- go run command:
go run example/hello
- go build command and run:
go build example/hello/main.go
./example/hello/main
variable type
1 | package main |
Key Points
- go语言是一门强类型语言,每一个变量都有它自己的变量类型
- go语言的字符串是内置类型,可以直接通过加号拼接,也能够直接用等于号去比较两个字符串。
- go语言变量的声明有两种方式
- 一种是通过
var name string = ""
这种方式来声明变量,声明变量的时候,一般会自动去推导变量的类型。 - 如果有需要,也可以显式写出变量类型。另一种声明变量的方式是:
使用变量 冒号 := 值
。
- 一种是通过
- 常量就是把var改成const,值得一提的是go语言里面的常量没有确定的类型,会根据使用的上下文来自动确定类型。
if else
1 | package main |
1 | 7 is odd |
Key Points
- if后面没有括号。
- if执行语句块必须接大括号。不能像C或者Cpp一样,直接把if里面的语句同一行。
for
1 | package main |
1 | loop |
Key Points
- go语言只有for循环
- 最简单的for后面什么也不写,是死循环
- for i等于0,i小于n,i加加。这中间三段,任何一段都可以省略。
switch
1 | package main |
1 | two |
Key Points
- go语言的switch后面的那个变量名,不要括号。
- 很大的一点不同的是,在cpp里面,switch case如果不加break的话会然后会继续往下跑完所有的case,在go语言里面的话不加break也会跳出来。
- 相比C或者Cpp,go语言里面的switch功能更强大。可以使用任意的变量类型,甚至可以用来取代任意的if else语句。你可以在switch后面不加任何的变量,然后在case里面写条件分支。这样代码相比你用多个if else代码逻辑会更为清晰。
array
1 | package main |
1 | 100 5 |
Key Points
- 数组就是一个具有编号且长度固定的元素序列。
- 对于一个数组,可以很方便地取特定索引的值或者往特定索引取存储值,然后也能够直接去打印一个数组。不过,在真实业务代码里面,很少直接使用数组,因为长度是固定的,用的更多的是切片。
Slice
在Go语言中,“slice"是对数组的一种抽象表示。你可以把它想象成从数组中"切下"来的一片,所以被称为"slice”(切片)。切片是动态的,它们可以根据需要自动增长和缩小,这与数组的固定大小形成对比。切片提供了一个更灵活,更强大的接口来处理序列类型的数据。
1 | package main |
Key Points
- 可以用make来创建一个切片,可以像数组一样去取值
- 使用append来追加元素。注意append的用法,必须把append的结果返回给原数组。因为slice的原理实际上是它有一个它存储了一个长度和一个容量,加一个指向一个数组的指针,在执行append操作的时候,如果容量不够的话,会扩容并且返回新的 slice。
- slice初始化的时候也可以指定长度。
- slice拥有像python一样的切片操作,比如取出第二个到第五个位置的元素,不包括第五个元素。不过不同于python的是不支持负数索引。
map
-
可以用make创建一个空map
1
m := make(map[string]int)