go 学习一

更多干货

https://github.com/csy512889371/learndemo/tree/master/go/golearn

• 变量

• 类型

• 常量

• 枚举

代码

package main

import (
"fmt"
"math"
"math/cmplx"
)

var (
aa = 3
ss = "kkk"
bb = true
)

func variableZeroValue() {
var a int
var s string
fmt.Printf("%d %q\n", a, s)
}

func variableInitialValue() {
var a, b int = 3, 4
var s string = "abc"
fmt.Println(a, b, s)
}

func variableTypeDeduction() {
var a, b, c, s = 3, 4, true, "def"
fmt.Println(a, b, c, s)
}

func variableShorter() {
a, b, c, s := 3, 4, true, "def"
b = 5
fmt.Println(a, b, c, s)
}

func euler() {
//复数
c := 3 + 4i
fmt.Println(cmplx.Abs(c))

fmt.Printf("%.3f\n",
cmplx.Exp(1i*math.Pi)+1)
}

func triangle() {
var a, b int = 3, 4
fmt.Println(calcTriangle(a, b))
}

func calcTriangle(a, b int) int {
var c int
c = int(math.Sqrt(float64(a*a + b*b)))
return c
}

func consts() {
const (
filename = "abc.txt"
a, b     = 3, 4
)
var c int
c = int(math.Sqrt(a*a + b*b))
fmt.Println(filename, c)
}

func enums() {
const (
cpp        = iota
_
python
golang
javascript
)

const (
b  = 1 << (10 * iota)
kb
mb
gb
tb
pb
)

fmt.Println(cpp, javascript, python, golang)
fmt.Println(b, kb, mb, gb, tb, pb)
}

func main() {
fmt.Println("Hello world")
//变量
variableZeroValue()
variableInitialValue()
variableTypeDeduction()
variableShorter()
fmt.Println(aa, ss, bb)

//欧拉公式
euler()
//强制类型转换
triangle()
//常量
consts()
//枚举
enums()
}