静态类型

静态类型(static type) 就是变量声明时候的类型。例如:

// int 是静态类型
var number int
// string 也是静态类型
var name string


动态类型

动态类型(concrete type) 是程序运行时系统才能看见的类型。例如:

// in 的静态类型为 interface{}
var in interface{}
// in 的静态类型为 interface{} 动态类型为 int
in = 100
// in 的静态类型为 interface{} 动态类型为 string
in = "菜籽爱编程"
ininterface{}intstring

接口组成

pair
var number int = 100
intnumber100
package main

import "fmt"

func main() {
 number := (int)(100)
 // 或者写成 number := (interface{})(100)
 fmt.Printf("number type: %T, data: %v", number, number)
}

运行上面的程序输出如下:

number type: int, data: 100

参考文献:

[1] Alan A. A. Donovan; Brian W. Kernighan, Go 程序设计语言, Translated by 李道兵, 高博, 庞向才, 金鑫鑫 and 林齐斌, 机械工业出版社, 2017.