package main import "fmt" type Cat struct { Name string } func (c Cat) Say() string {return c.Name + ":喵喵喵"} type Dog struct { Name string } func (d Dog) Say() string {return d.Name + ":汪汪汪"} func main() { c := Cat{Name: "小白猫"} // 小白猫:喵喵喵 fmt.Println(c.Say()) d := Dog{"阿黄"} fmt.Println(d.Say()) // 阿黄: 汪汪汪 } /* 小白猫:喵喵喵 阿黄: 汪汪汪 */
package main import "fmt" //1、接口是一个规范 type Usber interface { start() stop() } //2、如果接口里面有方法的话,必要要通过结构体或者通过自定义类型实现这个接口 func (p Phone) start() { fmt.Println(p.Name, "启动") } func (p Phone) stop() { fmt.Println(p.Name, "关机") } func main() { p := Phone{ Name: "华为手机", } var p1 Usber // golang中接口就是一个数据类型 p1 = p // 表示手机实现Usb接口 p1.start() p1.stop() } /* 华为手机 启动 华为手机 关机 */
package main import "fmt" //空接口作为函数的参数 func show(a interface{}){ fmt.Printf("值:%v 类型:%T\n",a,a) } func main() { show(20) // 值:20 类型:int show("你好golang") // 值:你好golang 类型:string slice := []int{1, 2, 34, 4} show(slice) // 值:[1 2 34 4] 类型:[]int }
pakcage main import "fmt" func main() { var slice = []interface{}{"Snail", 20, true, 32.2} fmt.Println(slice) // [Snail 20 true 32.2] }
package main import "fmt" func main() { //空接口作为 map 值 var studentInfo = make(map[string]interface{}) studentInfo["name"] = "Snail" studentInfo["age"] = 18 studentInfo["married"] = false fmt.Println(studentInfo) // [age:18 married:false name: Snail] }
package main import "fmt" func main() { var x interface{} x = "Hello GoLang" v,ok := x.(string) if ok { fmt.PrintIn(v) }else { fmt.PrintIn("非字符串类型") } }
package main import "fmt" type Usb interface { Start() Stop() } type Phone struct{ Name string } func (p Phone) Start(){ fmt.PrintIn(p.Name,"开始工作") } func (p Phone) Stop(){ fmt.PrintIn(p.Name,"停止") } func main() { phone1 := Phone{ //1、实例化值类型 Name:"小米手机" } var p1 Usb = phone1 //phone1实现了Usb接口phone1是Phone类型 p1.Start() phone2 := &Phone{ // 二:实例化指针类型 Name: "苹果手机", } var p2 Usb = phone2 //phone2 实现了 Usb 接口 pho }
package main import "fmt" type Usb interface { Start() Stop() } type Phone struct { Name string } func (p *Phone) Start() { fmt.Println(p.Name, "开始工作") } func (p *Phone) Stop() { fmt.Println("phone 停止") } func main() { /*错误写法 phone1 := Phone{ Name: "小米手机", } var p1 Usb = phone1 p1.Start() */ //正确写法 phone2 := &Phone{ // 指针类型接收者只能传入指针类型,不能传入值类型 Name: "苹果手机", } var p2 Usb = phone2 //phone2 实现了 Usb 接口 phone2 是 *Phone 类型 p2.Start() //苹果手机 开始工作 }
package main import "fmt" type AInterface interface { GetInfo() string } type BInterface interface { SetInfo(string, int) } type People struct { Name string Age int } func (p People) GetInfo() string { return fmt.Sprintf("姓名:%v 年龄:%d", p.Name, p.Age) } func (p *People) SetInfo(name string, age int) { p.Name = name p.Age = age } func main() { var people = &People{ Name: "Snail", Age: 20, } // people 实现了 AInterface 和 BInterface var p1 AInterface = people var p2 BInterface = people fmt.Println(p1.GetInfo()) p2.SetInfo("pupu", 21) // 姓名:Snail 年龄:20 fmt.Println(p1.GetInfo()) // 姓名:pupu 年龄:21 }
package main import "fmt" type SayInterface interface { say() } type MoveInterface interface { move() } // 接口嵌套 type Animal interface { SayInterface MoveInterface } type Cat struct { name string } func (c Cat) say() { fmt.Println("喵喵喵") } func (c Cat) move() { fmt.Println("猫会动") } func main() { var x Animal x = Cat{name: "花花"} x.move() // 猫会动 x.say() // 喵喵喵 }