go interface

例一:

package main

import "fmt"

type USB interface {
	Name() string
	Connect()
}

type PhoneConnector struct {
	name string
}

func (pc PhoneConnector) Name() string {
	return pc.name
}

func (pc PhoneConnector) Connect() {
	fmt.Println("Connected:", pc.name)
}

func main() {
	a := PhoneConnector{"PhoneConnector"}
	a.Connect()
	Disconnect(a)
}

func Disconnect(usb USB) {
	fmt.Println("Disconnected.")
}

例二:

package main

import "fmt"

type USB interface {
	Name() string
	Connector
}

type Connector interface {
	Connect()
}

type PhoneConnector struct {
	name string
}

func (pc PhoneConnector) Name() string {
	return pc.name
}

func (pc PhoneConnector) Connect() {
	fmt.Println("Connected:", pc.name)
}

func main() {
	a := PhoneConnector{"PhoneConnector"}
	a.Connect()
	Disconnect(a)
}

func Disconnect(usb USB) {
	fmt.Println("Disconnected.")
}

例三:

package main

import "fmt"

type USB interface {
	Name() string
	Connector
}

type Connector interface {
	Connect()
}

type PhoneConnector struct {
	name string
}

func (pc PhoneConnector) Name() string {
	return pc.name
}

func (pc PhoneConnector) Connect() {
	fmt.Println("Connected:", pc.name)
}

func main() {
	a := PhoneConnector{"PhoneConnector"}
	a.Connect()
	Disconnect(a)
}

func Disconnect(usb USB) {
	if pc, ok := usb.(PhoneConnector); ok {
		fmt.Println("Disconnected.", pc.name)
		return
	}
	fmt.Println("unknown device.")

}

参数为空接口的处理方法:

	package main

import "fmt"

type USB interface {
	Name() string
	Connector
}

type Connector interface {
	Connect()
}

type PhoneConnector struct {
	name string
}

func (pc PhoneConnector) Name() string {
	return pc.name
}

func (pc PhoneConnector) Connect() {
	fmt.Println("Connected:", pc.name)
}

func main() {
	a := PhoneConnector{"PhoneConnector"}
	a.Connect()
	Disconnect(a)
}

func Disconnect(usb interface{}) {

	switch v := usb.(type) {
	case PhoneConnector:
		fmt.Println("Disconnected", v.name)
	default:
		fmt.Println("Unknown device.")
	}

}

在刚才的基础上修改main函数

package main

import "fmt"

type USB interface {
	Name() string
	Connector
}

type Connector interface {
	Connect()
}

type PhoneConnector struct {
	name string
}

func (pc PhoneConnector) Name() string {
	return pc.name
}

func (pc PhoneConnector) Connect() {
	fmt.Println("Connected:", pc.name)
}

func main() {
	pc := PhoneConnector{"PhoneConnector"}
	var a Connector
	a = Connector(pc)
	a.Connect()
}

func Disconnect(usb interface{}) {

	switch v := usb.(type) {
	case PhoneConnector:
		fmt.Println("Disconnected", v.name)
	default:
		fmt.Println("Unknown device.")
	}

}

无法互相转化的情况:

package main

import "fmt"

type USB interface {
	Name() string
	Connector
}

type Connector interface {
	Connect()
}

type PhoneConnector struct {
	name string
}

func (pc PhoneConnector) Name() string {
	return pc.name
}

func (pc PhoneConnector) Connect() {
	fmt.Println("Connected:", pc.name)
}

type TVConnector struct {
	name string
}

func (tv TVConnector) Connect() {
	fmt.Println("Connected:", tv.name)
}

func main() {
	tv := TVConnector{"TVConnector"}
	var a USB
	a = USB(tv)
	a.Connect()
}

func Disconnect(usb interface{}) {

	switch v := usb.(type) {
	case PhoneConnector:
		fmt.Println("Disconnected", v.name)
	default:
		fmt.Println("Unknown device.")
	}

}

将对象赋值给接口时,会发生拷贝,而接口内部存储的是指向这个复制品的指针,既无法修改复制品的状态,也无法获取指针

package main

import "fmt"

type USB interface {
	Name() string
	Connector
}

type Connector interface {
	Connect()
}

type PhoneConnector struct {
	name string
}

func (pc PhoneConnector) Name() string {
	return pc.name
}

func (pc PhoneConnector) Connect() {
	fmt.Println("Connected:", pc.name)
}

func main() {
	pc := PhoneConnector{"PhoneConnector"}
	var a Connector
	a = Connector(pc)
	a.Connect()
	//完全无视对原来对象的修改,因此他得到的是一个拷贝
	pc.name = "pc"
	a.Connect()
}

func Disconnect(usb interface{}) {

	switch v := usb.(type) {
	case PhoneConnector:
		fmt.Println("Disconnected", v.name)
	default:
		fmt.Println("Unknown device.")
	}

}

只有当接口存储的类型和对象都为nil时,接口才等于nil

package main

import "fmt"

func main() {
	var a interface{}
	fmt.Println(a == nil)
	var p *int = nil
	a = p
	fmt.Println(a == nil)
}