Go语言文件基础语法
package main //管理定义模块
import "fmt" //导入模块
/*
主函数
打印Hello,world!
*/
func main() {
fmt.Println("Hello,world!")
}
var变量
3种变量声明方式:
package main
import "fmt"
func main() {
var a string
a = "a"
var b = "b"
c := "c"
var(
d string
e = "e"
)
d = "d"
fmt.Println(a, b, c, d, e)
}
const常量
iota实现枚举
package main
import "fmt"
func main() {
const a = "a"
const b = 1
const (
c = iota
d
e
)
const (
f = (iota + 1) * 2 // iota每次const均重置
g
h
)
fmt.Println(a, b, c, d, e, f, g, h) //a 1 0 1 2 2 4 6
}
if判断与for循环
判断与多种循环的方法
package main
import (
"fmt"
"time"
)
func main() {
for i := 0; i < 4; i++ {
if i < 1 {
j := 0
for ; j < 3; j++ {
fmt.Print(j) //012
}
} else if i == 1 {
j := 1
for j < 4 {
fmt.Print(j) //123
j++
}
} else if i == 2 {
for k, v := range "abc" {
fmt.Printf("%d %c,", k, v) //0 a,1 b,2 c,
}
} else {
for {
time.Sleep(1000000000) // 等待若干纳秒(1000000000纳秒=1秒)
fmt.Println("死循环")
}
}
}
}
基本数据类型
package main
import "fmt"
func main() {
var (
// 整型
// 无符号整型
a uint8 = 1
b uint16 = 2
c uint32 = 3
d uint64 = 4
// 有符号整型
e int8 = -1
f int16 = -2
g int32 = -3
h int64 = -4
//uint和int是32位还是64位具体看操作系统
i uint = 0
j = 1 //Go语言默认为int
k = uintptr(2) //指针
//浮点型
l = 0.123 //Go语言默认为float64
m float32 = 0.234
//复数
n complex64 = complex(1, 2)
o = complex(3, 4) //Go语言默认为complex128
p = "abc" //字符串string
q = 0777 //八进制
r = 0xff //十六进制
)
//布尔值bool:true和false
fmt.Println(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, true, false)
fmt.Printf("%T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T, %T", a, b, c, d, e,
f, g, h, i, j, k, l, m, n, o, p, q, r, true, false)
}