Golang 教程1
注意,该文档只适合有编程基础的同学,这里的go教程只给出有区别的知识点
[go教程]('Go基础 · Go语言中文文档 (topgoer.com)')
一、基础
tip: 下划线_ 可以起到忽略的作用
运行以下命令来初始化一个新的 Go 模块:
go
// 如你的项目在 gocode/testproject01/unit2/demo01 中 (从src后开始, 用'/'隔开)。
go mod init gocode/testproject01/unit2/demo01
//1、go mod init:这是 Go 的模块初始化命令,用于创建一个新的 go.mod 文件,它是 Go 模块的元数据文件,用于记录模块的基本信息和依赖关系。
//2、gocode/testproject01/unit2/demo01:这是你指定的模块名称。这个名称将成为你的模块在 Go 生态系统中的唯一标识符。
//输入操作
_, err := fmt.Scanf("%v", &name) //录入数据的时候,类型一定要匹配。
// _:这是一个空白标识符,在 Go 语言中用来忽略赋给它的值。
//1、 fmt.Scanln 函数返回两个值:第一个是扫描的元素数量,第二个是错误(如果有)。因为我们只关心错误(第二个返回值),所以我们使用 _ 来忽略第一个返回值。
//2、 err:这是用来存储 fmt.Scanln 函数返回的错误值。如果函数执行成功,则 err 将为 nil;如果发生错误,则 err 将包含具体的错误信息。
//3、 :=:这是一个短声明语法,用于声明并初始化一个变量。在这里,我们声明了一个名为 err 的变量,并将其初始化为 fmt.Scanln(&a) 函数的第二个返回值。
//4、 fmt.Scanln(&a):这是 fmt.Scanln 函数调用,它从标准输入读取数据,并尝试将读取的数据转换成变量 a 的类型(这里是整型 int)。它需要一个变量的引用作为参数,这里使用了 &a。
1、 自动缩进
go
gofmt -w xxx.go
2、 关于赋值, 支持这种声明
go
sex := "man"
var n4, name, n5 = 10, "jack", 7.8
n6, height := 6.9, 100.6
3、全局赋值, 省时省力
go
var (
n9 = 100
n10 = "dasda"
)
//导入包也可以如此操作, 如
import (
"fmt"
"strconv"
)
4、字符串一旦定义好,值不可变
go
name = "黎明"
name[0] = "牛" //会报错
5、 如果字符串需要赋值特殊的字符(关键字、<、= ...),用反单引号包裹
go
str := `
var a string = 1
string a < 1231
`
6、类型转换 T() 显示转化, T为你想要转换的类型
go
a := 100
b := float32(a)
fmt.Println(a, b)
//注意,类型大的转化至类型小的变量,高位部分会被截断
var n1 int64 = 888888888
var n2 = int8(n1)
fmt.Println(n2)
fmt.Printf("%T", n2c)
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
56
int8
``````````````````````````````````````````````````````````````````````````````````````````````````
var n3 int32 = 12
//会报错, var n4 int64 = int64(n3) + 30
var n4 int8 = n3 + 30
fmt.Println(n3)
fmt.Println(n4)
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
# command-line-arguments
.\main.go:5:16: cannot use n3 + 30 (value of type int32) as int8 value in variable declaration
``````````````````````````````````````````````````````````````````````````````````````````````````
7、 string类型转化,strconv.parseInt等函数返回的是2个值(value, err)所以应有2个变量承接
go
func main() {
str := "123"
i, err := strconv.Atoi(str)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(i)
}
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
123
``````````````````````````````````````````````````````````````````````````````````````````````````
8、 指针
go
func main() {
var a int = 10
var ptr *int = &a
fmt.Println("Value of a is ", a)
fmt.Println("Value of a is ", *ptr)
fmt.Println("Address of a is ", ptr)
fmt.Println("Address of a is ", &a)
fmt.Println("Address of a is ", &ptr)
*ptr = 20
fmt.Println("Value of a is ", a)
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
Value of a is 10
Value of a is 10
Address of a is 0xc00000a0f8
Address of a is 0xc00000a0f8
Address of a is 0xc000078050
Value of a is 20
`````````````````````````````````````````````````````````````````````````````````````````````````
9、分支控制
go
//单、多分支, 注意:条件表达式可以不写,也建议不写
if 条件表达式 {
逻辑代码
} else if 条件表达式 {
逻辑代码
} else {
逻辑代码
}
// switch 不用额外加break 防止代码穿透
switch 条件表达式 {
case 条件满足1:
xxx
case 条件满足2:
xxx
。。。
default:
XXX
}
// 如:
func main() {
var a int
fmt.Println("请输入数字:")
_, err := fmt.Scanln(&a)
if err != nil {
fmt.Println("输入错误:", err)
return
}
if a == 1 {
fmt.Println("a = 1")
} else if a == 2 {
fmt.Println("a = 2")
} else {
fmt.Println("a = 3")
}
}
//可以使用 fallthrough 穿透,但只能穿透一层
10、 循环 (go里面没有while循环)
go
//跟c语言一样,条件表达式不用()包裹
func main() {
i, sum := 0, 0
for i = 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
45
``````````````````````````````````````````````````````````````````````````````````````````````````
//可以跟while一样, 格式灵活
func main() {
for {
fmt.Println("hello Golang")
}
}
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
hello Golang
hello Golang
hello Golang
hello Golang
hello Golang
。。。
``````````````````````````````````````````````````````````````````````````````````````````````````
10.1 for range (Go 特有,可以遍历数组、切片、字符串、map以及通道,形式上有点类似于foreach)
go
func main() {
str := "hello world"
for i := 0; i < len(str); i++ {
fmt.Printf("%c", str[i])
}
}
//等效于
// 这里的 fmt.Printf("索引为 %d, 对应的值为 %c \n", i, val) 是带格式的输出, 可以类比 fmt.Println()
func main() {
str := "hello world"
for i, val := range str {
fmt.Printf("索引为 %d, 对应的值为 %c \n", i, val)
}
}