原文链接:https://www.2cto.com/kf/201712/703563.html 1. 用于判断变量类型 demo如下: switch t := var.(type){ case string: //add your operations case int8: //add your operations case int16: //add your operations default: return errors.New("no this type") } } //空接
接口定义 Interface类型可以定义一组方法,但是这些不需要实现.并且interface不能 包含任何变量. type Interface interface { test1(a, b int) bool test2() } interface类型默认是一个指针. 空接口(a interface{})可以被任何类型所实现,如动物可以被猫.狗.人等实现,反之人不一定能实现动物 func main() { var a interface{} var b int a = b fmt.Printf(
新的公司,新的氛围.一年了,打算写点什么.so,那就写google的golang语言吧. 最最最基础的语法结构见go语言菜鸟教程 接下来写点菜鸟教程没有的. go语言的设计者认为:go语言必须让程序员写出什么代码就得出什么结果.为了这个目标,把foreach循环原本默认从下标0开始的硬改成了从随机下标开始. go语言是一个强类型的语言,所以类型转换是必不可少的.不同类型的数据强制要求你手动转换成相同类型. var a = // 默认是int类型 var b = int64() fmt.Sprin
原文链接:http://www.52bd.net/code/210.html demo: package main import ( "fmt" ) //通知行为的接口 type notifier interface{ notify() } //自定义的用户类型 type user struct{ name string email string } //notify是用指针接收者实现的方法 func (u *user ) notify(){ fmt.Println("用户:
比如,我们定义了一个 struct type person struct { Name string `json:"name"` Age int `json:"age"` } 然后有一个函数为了通用性,函数返回值类型为 interface,但是某种情况我们知道这个函数是返回 person 类型的,我们就可以 person.(person).Name 来调用 person 类型里面的东西,因为 interface 类型直接调用会报错. 参考:Type_assertio
先看一下一段关于defer的解释, 引自<Go程序设计语言> Syntactically, a defer statement is an ordinary function or method call prefixed by the keyword defer.The function and argument expressions are evaluated when the statement is executed, but the actual call is deferred