Go语言第一天| 青训营笔记

79 阅读2分钟

Go语言第一天| 青训营笔记

什么是Go语言? 他有以下优点: 1.高性能、高并发 2.语法简单、学习曲线平缓 3.丰富的标准库 4.完善的工具链 5.静态链接 6.快速编译 7.跨平台 8.垃圾回收 相比较于C语言等其他语言,更好上手

Go语言可以自动推导变量类型: var a = "initial" var b, c int = 1, 2 var d = true var e float64 f:= float32(e)

可以跟a一样无需设定类型,也可以和bc一样定义为整数型。

const s string ="constant' const h = 500000000 const i = 3e20 / h 相同的,上面常量也可以根据上下文自动设定类型。

Golang中的if else使用: if 7%2=0{ fmt.Println("7 is even" ) }else { fmt.Println("7 is odd" ) }

if 8%4 == 0 { fmt.Println( "8 is divisible by 4") } if num := 9; num < 0 { fmt.Printin(num,"is negative" ) } else if num < 10 { fmt .Println(num,"has 1 digit") } else { fmt.Println(num,"has multiple digits") }

条件无需加括号,执行语句与判断语句不能写在同一行。

Golang中的for循环:

i:=1 for { fmt.Printin("loop" ) break } for j := 7;j< 9;j++{ fmt.PrintIn(j) } for n := 0;n <5;n++{ if n%2 == 0 { continue } fmt.Printin(n) } for i <= 3{ fmt.Printin(i) i=i+1 } }

创建数组: var a [5]int a[4] = 100 fmt.Printin(a[4], len(a)) b := [5]int(1, 2,3,4,5) fmt.Printin(b)

数组长度固定,更多用的是切片,使用make创建一个切片: s: make([]string,3) s[0]="a" s[1]="b" s[2]="c" fmt.Println("get:",s[2]) fmt.PrintIn("len:",len(s)) // 3

s = append(s,"d") s = append(s,"e","f") fmt.Println(s) // [a b c d e f]

c := make([]string, len(s)) copy(c,s) fmt.Println(c) // [a b c d e f] fmt.Println(s[2:5]) // [c d e] fmt.Println(s[:5]) //[a b c d e] fmt,PrintIn(s[2:]) //[c d e f]

Golang函数创建: func add(a int, b int) int { return a + b } 其中函数名为add

Golang的指针

Golang结构体: type user struct { name string password string }

Golang字符串操作: contain 判断 返回True False Count 计算数量 Index 查找位置 等等...