packagemainimport("fmt")type Peopleinterface{Speak(string)string}type Stduent struct{}func(stu*Stduent)Speak(think string)(talk string){ifthink=="love"{talk="You are a good boy"}else{talk="hi"}return}funcmain(){
首先interface 是一种类型,从它的定义中就可以看出用了 type 关键字,更准确的说 interface 是一种具有一组方法的类型,这些方法定义了 interface 的行为。Go 允许不带任何方法的 interface, 这种类型的 interface 叫 empty interface。如果一个类型实现了一个 interface 中所有的方法,我们说该类型实现了该 interface,...
go tool objdump -s "main\.dataInterface" if (汇编会因平台不同,调用结果会不一样)调用了 CALL runtime.XXX 对应代码在runtime/iface.go下,在这里,我们看到了interface的两个核心结构 eface 和 iface,分别对应convT2E和convT2I type iface struct { tab *itab data unsafe.Pointer } type eface struct...
nodeper1楼
kindUint8 kindUint16 kindUint32 kindUint64 kindUintptr kindFloat32 kindFloat64 kindComplex64 kindComplex128// 数组kindArray// 通道kindChan// 函数kindFunc// 接口kindInterface// mapkindMap// 指针kindPtr// 切片kindSlice// 字符串kindString// 结构体kindStruct// Pointer指针kindUnsafePointer ...
var i interface{} = "hello" s, ok := i.(string) if ok { fmt.Println(s) // 输出 "hello" } else { fmt.Println("not a string") } 可以看出,interface{} 通过运行阶段的装箱拆箱操作实现了多种数据类型的支持操作。 2)泛型实现原理 Go 核心团队在评估 Go 泛型实现方案时非常谨慎,一共提交了...
type Animal interface { Eat(string) string Drink(string) string } type Cat struct{} func ...
1type Scheduleinterface{2// Return the next activation time, later than the given time.3// Next is invoked initially, and then each time the job is run.4// 返回同一 Entity 中的 Job 下一次执行的时间5Next(time.Time)time.Time6}
name string } func main() { var stuChan chan *student stuChan = make(chan *student, 10) stu := student{name: "stu001"} stuChan <- &stu var stu01 interface{} stu01 = <-stuChan var stu02 *student stu02, ok := stu01.(*student) ...
uintptr 4或8 以存储指针的 uint32 或 uint64 整数 array 值类型 struct 值类型 string "" UTF-8 字符串 slice nil 引用类型 map nil 引用类型 channel nil 引用类型 interface nil 接口 function nil 函数 nil 空指针 3.1 格式化打印 fmt包支持如下几种打印方式 fmt.Println:打印一行内容,类似std::cout,难...