go语言的特点
高并发,高性能;语法简单,学习曲线平缓;有丰富的标准库;完善的工具链; 静态链接;编译快速;跨平台;垃圾回收
golang开发环境安装
go.dev/ 官网下载Golang VSCODE/GOLAND 前者是代码编辑器但是加入插件后也可以作为功能齐全的IDE使用 后者是商业收费的,但是可以有30天免费试用和学生认证免费
golang基础语法
hello world:
package main
import (
"fmt"
)
func main() {
fmt.Println("hello world")
}
变量:
go的变量支持整数,浮点数,布尔类型,字符串,
把var改为const则是常量
go中变量类型会自动更具上下文决定类型
package main
import (
"fmt"
"math"
)
func main() {
var a = "initial"
var b, c int = 1, 2
var d = true
var e float64
f := float32(e)
g := a + "foo"
fmt.Println(a, b, c, d, e, f) // initial 1 2 true 0 0
fmt.Println(g) // initial apple
const s string = "constant"
const h = 500000000
const i = 3e20 / h
fmt.Println(s, h, i, math.Sin(h), math.Sin(i))
}
循环:
go中只有for循环,可以三段式写循环变量的初始化,循环条件和变化
break和continue来跳出循环或者继续
package main
import "fmt"
func main() {
i := 1
for {
fmt.Println("loop")
break
}
for j := 7; j < 9; j++ {
fmt.Println(j)
}
for n := 0; n < 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
for i <= 3 {
fmt.Println(i)
i = i + 1
}
}
switch 和 if 条件判断语句:
if条件无需括号,switch每个分支不用break
switch中case可接条件语句
package main
import (
"fmt"
"time"
)
func main() {
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.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
a := 2
switch a {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
case 4, 5:
fmt.Println("four or five")
default:
fmt.Println("other")
}
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
}
数组:
数组可以指定访问目的内容,但是长度固定,一般使用切片slice
package main
import "fmt"
func main() {
var a [5]int
a[4] = 100
fmt.Println("get:", a[2])
fmt.Println("len:", len(a))
b := [5]int{1, 2, 3, 4, 5}
fmt.Println(b)
var twoD [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
}
slice就留到下一篇来进行展示记录吧