Go基础及语法(一)
Go语言文件基础语法
package main //管理定义模块import "fmt" //导入模块/*主函数打印Hello,world!*/func main() {fmt.Println("Hello,world!")}
var变量
3种变量声明方式:
package mainimport "fmt"func main() {var a stringa = "a"var b = "b"c := "c"var(d stringe = "e")d = "d"fmt.Println(a, b, c, d, e)}
const常量
iota实现枚举
package mainimport "fmt"func main() {const a = "a"const b = 1const (c = iotade)const (f = (iota + 1) * 2 // iota每次const均重置gh)fmt.Println(a, b, c, d, e, f, g, h) //a 1 0 1 2 2 4 6}
if判断与for循环
判断与多种循环的方法
package mainimport ("fmt""time")func main() {for i := 0; i < 4; i++ {if i < 1 {j := 0for ; j < 3; j++ {fmt.Print(j) //012}} else if i == 1 {j := 1for j < 4 {fmt.Print(j) //123j++}} 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 mainimport "fmt"func main() {var (// 整型// 无符号整型a uint8 = 1b uint16 = 2c uint32 = 3d uint64 = 4// 有符号整型e int8 = -1f int16 = -2g int32 = -3h int64 = -4//uint和int是32位还是64位具体看操作系统i uint = 0j = 1 //Go语言默认为intk = uintptr(2) //指针//浮点型l = 0.123 //Go语言默认为float64m float32 = 0.234//复数n complex64 = complex(1, 2)o = complex(3, 4) //Go语言默认为complex128p = "abc" //字符串stringq = 0777 //八进制r = 0xff //十六进制)//布尔值bool:true和falsefmt.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)}
赞 (0)