go基础语法学习02

go基础语法学习02

1. switch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
"fmt"
"os"
)

func main() {

cmds := os.Args

for key, val := range cmds {
fmt.Println("key:", key, "val:", val, "cmds len:", len(cmds))
}

switch cmds[1] {
case "hello":
fmt.Println("hello")
fallthrough
case "world":
fmt.Println("world")
default:
fmt.Println("default")
}
}
  1. os.Args 表示获取程序的输入参数,类似于C++的 argv**

  2. go的switch默认不需要加break,编译器会自动补上,如果使用fallthrough,编译器则不会给该case下补break

2. 标签 ‘LABLE’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import "fmt"

func main() {
LABEL1:
for i := 0; i < 5; i++ {
for j := 0; j < 5; j++ {
if j == 3 {
//break LABEL1
//goto LABEL1
continue LABEL1
}

fmt.Println("i:", i, " j:", j)
}
}

fmt.Println("over")
}
  1. break+标签:表示跳出标签下的循环。
  2. continue+标签:表示跳过标签下的当前循环
  3. goto+标签:表示程序从标签位置重新执行

3. 枚举-iota

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import "fmt"

//在Go语言中没有枚举类型,通过const+iota(常量累加器)来进行模拟
const (
MONDAY = iota + 2 //iota = 0
TUESDAY // iota = 1
WEDNESDAY // iota = 2
THURSDAY // iota = 3
FRIDAY // iota = 4
X, Y = iota, iota // iota = 5
SATURDAY = 1 // iota = 6
SUNDAY // iota = 7
M, N = iota, iota // iota = 8
)

const (
first = 1
second
third
fourth = iota
)

func main() {
fmt.Println(MONDAY)
fmt.Println(TUESDAY)
fmt.Println(WEDNESDAY)
fmt.Println(THURSDAY)
fmt.Println(FRIDAY)
fmt.Println(SATURDAY)
fmt.Println(SUNDAY)
fmt.Println(X, Y)
fmt.Println(M, N)
fmt.Println(first, second, third, fourth)
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
1
1
5 5
8 8
1 1 1 3

iota 常量累加器,遇到const就置 0,换行+1

iota只能在const作用域内使用

const之后每换一行iota+1,即使没有使用

4. 结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import "fmt"

type Student struct {
name string
age int
gender string
score float64
}

func main() {
tokey := Student{
"tokey",
20,
"男",
80.001,
} // 等价于 tokey := Student{"tokey", 20, "男", 80.001} 注意结尾少了个逗号

// tokey := Student{
// name: "tokey",
// age: 20,
// gender: "男",
// score: 80.001,
// }
fmt.Println("tokey:", tokey.name, tokey.age, tokey.gender, tokey.score)

s1 := &tokey

fmt.Println("指针 tokey:", s1.name, s1.age, s1.gender, (*s1).score)
//go语言中没有->这种写法,指针也是使用.操作符
}

5. init函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//day02\05-init.go
package main

import (
ADD "day02/05-import/add"
"day02/05-import/sub"

// 一个包中有多个init函数时,都会调用,但是顺序不能保证
// _ "day02/05-import/sub" //这样只会调用init函数,其他函数无法调用
"fmt"
)

func init() {
fmt.Println("this is 05-init func")
}

func main() {
fmt.Println("sub:", sub.Sub(40, 12), "add:", ADD.Add(12, 12))
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//day02\05-import\add\add.go
package add

import "fmt"

func init() {
fmt.Println("this is init() in the package add")
}

func Add(a int, b int) int {
fmt.Println("public:", a+b)
return a + b
}

func add(a, b int) int {
fmt.Print("protected:", a+b)
return a + b
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//day02\05-import\sub\sub.go
package sub

import "fmt"

func init() {
fmt.Println("this is first init() in package sub")
}

func init() {
fmt.Println("this is second init() in package sub")
}

func Sub(first int, second int) int {
fmt.Println("public:", first-second)
return first - second
}

func sub(first, second int) int {
fmt.Println("protected:", first-second)
return first - second
}
1
2
3
4
5
6
7
this is init() in the package add
this is first init() in package sub
this is second init() in package sub
this is 05-init func
public: 28
public: 24
sub: 28 add: 24

6. defer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
"fmt"
"os"
)

//资源清理
//在同一个函数中多次调用defer,执行时类似于栈的机制,先进后出
func main() {
readFile("01-switch.go")
}

func readFile(fileName string) {
f1, err := os.Open(fileName)
defer func(fileName string) {
fmt.Println("ready close file:", fileName)
ret := f1.Close()
fmt.Println("close file ret:", ret)
}(fileName) //最后的()表示调用 之前表示声明
if err != nil {
fmt.Println("os.Open ", fileName, " error:", err)
return
}

buf := make([]byte, 1024)
n, _ := f1.Read(buf)

defer fmt.Println("11111")
defer fmt.Println("22222")

fmt.Println("read file real length:", n)
//fmt.Println("file content is:", buf) //buf是二进制数据
//fmt.Println("file content is:", string(buf))
}
1
2
3
4
5
read file real length: 343
22222
11111
ready close file: 01-switch.go
close file ret: <nil>

本文标题:go基础语法学习02

文章作者:Tokey

发布时间:2021年06月27日 - 08:06

最后更新:2021年06月29日 - 22:06

原始链接:http://TokeyRoad.github.io/2021/06/27/go基础语法学习02/

许可协议: 转载请保留原文链接及作者。

0%