GoCCGoC
JavaScriptvarJavaScriptGo
变量
var v int // 声明变量 v,自动初始化值为零值(int 类型为 0)
v = 1 // 给变量 v 赋值
v := 1 // 声明变量 v,并初始化值为 1,编译器自动推导类型为 int
:=v
多重赋值
基本的赋值方法都是类似的,但 Go 提供了动态语言中才有的多重赋值功能。
i, j = j, i
匿名变量
函数的返回值中可能只有一个感兴趣的,别的值都不需要,是否还需要定义变量去接收这几个返回值?使用匿名变量!
func GetName() (firstName, lastName, nickName string) {
return "May", "Chan", "Chibi"
}
_, _, nickName := GetName()
常量
常量值必须是编译期可确定的数字、字符串、布尔值。即常说的字符字面量(literal),根据字面量可以推测出常量的类型。
const x,y int = 1, 2 // 多常量初始化
const Pi float64 = 3.1415926
const zero = 0.0 // 类型推断
const ( // 常量组
size int64 = 1024
eof = -1 // 类型推断
Eof // 在常量组中,如果不提供类型和初始化值,那么视作与上⼀常量相同。
)
iota
iotaconst0constiota1
const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
)
枚举
Go
type Color int // 定义类型 Color,传递 Color 的地发不能传递 int
const (
Black Color = iota // 定义常量 Black = 0
Red
Blue
)
func test(c Color) {}
func main() {
c := Black
test(c)
x := 1
test(x) // Error: cannot use x (type int) as type Color in function argument
test(1) // 常量会被编译器自动转换
基本类型
| 类型 | ⻓度 | 默认值(零值) | 说明 |
|---|---|---|---|
| bool | 1 | false | |
| byte | 1 | 0 | uint8 |
| rune | 4 | 0 | Unicode Code Point, int32 |
| int, uint | 4 或 8 | 0 | 32 或 64 位 |
| int8, uint8 | 1 | 0 | -128 ~ 127, 0 ~ 255 |
| int16, uint16 | 2 | 0 | -32768 ~ 32767, 0 ~ 65535 |
| int32, uint32 | 4 | 0 | -21亿 ~ 21 亿, 0 ~ 42 亿 |
| int64, uint64 | 8 | 0 | |
| float32 | 4 | 0.0 | |
| float64 | 8 | 0.0 | |
| complex64 | 8 | ||
| complex128 | 16 | ||
| uintptr | 4 或 8 | ⾜以存储指针的 uint32 或 uint64 整数 | |
| array | 值类型 | ||
| struct | 值类型 | ||
| string | "" | UTF-8 字符串 | |
| slice | nil | 引⽤类型 | |
| map | nil | 引⽤类型 | |
| channel | nil | 引⽤类型 | |
| interface | nil | 接⼝ | |
| function | nil | 函数 |
int、uint、uintpter
格式化打印类型信息和类型的技巧。
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
)
func main() {
const f = "%T(%v)\n"
fmt.Printf(f, ToBe, ToBe)
fmt.Printf(f, MaxInt, MaxInt)
}
引用类型
引用类型包括 slice、map 和 channel。它们有复杂的内部结构,除了申请内存外,还需要初始化相关的属性。
new
make
a := []int{0, 0, 0} // 提供初始化表达式。
a[1] = 10
b := make([]int, 3) // 初始化长度为 3 的 slice
b[1] = 10
c := new([]int)
c[1] = 10 // Error: invalid operation: c[1] (index of type *[]int)
类型转换
不支持隐式类型转化,即便是从窄向宽转换也不行。
T(v)vT
Bool
BoolBool
Float
==
import "math"
// p 为自定义精度
func IsEqual(f1, f2, p float64) bool {
return math.Fdim(f1, f2) < p
}
字符串
字符串内容在初始化后不可变!
""s[i]&s[i]struct String {
byte* str; // 指向字节数组的指针
intgo len; // 长度
}
⽀持⽤两个索引号返回⼦串。⼦串依然指向原字节数组,仅修改了指针和⻓度属性。
s := "Hello, World!"
s1 := s[:5] // Hello
s2 := s[7:] // World!
s3 := s[1:5] // ello
字符串遍历
// 以字节数组遍历
str := "Hello,世界"
n := len(str)
for i := 0; i < n; i++ {
ch := str[i] // 取出下标上的字符,类型为 byte
}
// 每个中文字符在 UTF-8 占 3 个字节
// 以 Unicode 字符遍历
for i, ch := range str {
fmt.Println(i, ch) // ch 的类型为 rune
}
修改字符串
Unicode Code Point\uFFFF、\U7FFFFFFF、\xFFruneUCS-4
func main() {
fmt.Printf("%T\n", 'a')
var c1, c2 rune = '\u6211', '们'
println(c1 == '我', string(c2) == "\xe4\xbb\xac")
}
输出
int32 // rune 是 int32 的别名
true true
[]rune[]bytestring
func main() {
s := "abcd"
bs := []byte(s)
bs[1] = 'B'
println(string(bs)) // aBcd
u := "电脑"
us := []rune(u)
us[1] = '话'
println(string(us)) // 电话
}
指针
*TTnil
&
*
CGo
unsafe.Pointer
func main() {
x := 0x12345678
p := unsafe.Pointer(&x) // *int -> Pointer
n := (*[4]byte)(p) // Pointer -> *[4]byte 转换成数组指针
// 78 45 34 12
for i := 0; i < len(n); i++ {
fmt.Printf("%X ", n[i])
}
}
局部变量的指针
GC Heap
func test() *int {
x := 100
return &x // 在堆上分配 x 内存。但在内联时,也可能直接分配在⺫标栈。
}
变相实现指针运算
Pointeruintptr
func main() {
d := struct {
s string
x int
}{"abc", 100}
p := uintptr(unsafe.Pointer(&d)) // *struct -> Pointer -> uintptr
p += unsafe.Offsetof(d.x) // uintptr + offset
p2 := unsafe.Pointer(p) // uintptr -> Pointer
px := (*int)(p2) // Pointer -> *int
*px = 200 // d.x = 200
fmt.Printf("%#v\n", d)
}
输出
struct { s string; x int }{s:"abc", x:200}
注意:GC 把 uintptr 当成普通整数对象,它⽆法阻⽌ "关联" 对象被回收。
自定义类型可将类型分为命名和非命名两大类。
bool、int、string
array、slice、map
具有相同声明的未命名类型被视为同一类型
arrayslicemapchannelstructfunctioninterfacevar a struct { x int `a` }
var b struct { x int `ab` }
// cannot use a (type struct { x int "a" }) as type struct { x int "ab" } in assignment
// 结构体标签不同
b = a
Type
type
func main() {
type bigint int64
var x bigint = 100
println(x)
}
新类型不是原类型的别名,除拥有相同数据存储结构外,它们之间没有任何关系,不会持有原类型任何信息。
除⾮目标类型是未命名类型,否则必须显式转换。
x := 1234
var b bigint = bigint(x) // 必须显式转换,除⾮是常量。
var b2 int64 = int64(b)
// slice 只要存储相同类型的值
var s myslice = []int{1, 2, 3} // 未命名类型,隐式转换。
var s2 []int = s