package main
import (
"fmt"
)
//define a interface
type USB interface {
Name() string
connecter
}
type connecter interface {
connect()
}
type PhoneConnecter struct {
name string
}
func (pc PhoneConnecter) Name() string {
return pc.name
}
func (pc PhoneConnecter) connect() {
fmt.Println("Connected", pc.name)
}
func main() {
pc := PhoneConnecter{"PhoneConnecter"}
var a connecter
a = PhoneConnecter(pc)
a.connect()
//DisConnect(a)
//Jude(a)
var y interface{}
fmt.Print(y)
var p *int = nil
y = p
fmt.Println(y == nil)
}
func DisConnect(usb USB) {
if pc, ok := usb.(PhoneConnecter); ok {
fmt.Println("This is a PhoneConnecter", pc.name)
return
}
fmt.Println("Unknown device")
}
func Jude(usb USB) {
switch v := usb.(type) {
case PhoneConnecter:
fmt.Println("Jude from PhoneConnecter", v.name)
default:
fmt.Println("Unknown device.")
}
}
import (
"fmt"
)
//define a interface
type USB interface {
Name() string
connecter
}
type connecter interface {
connect()
}
type PhoneConnecter struct {
name string
}
func (pc PhoneConnecter) Name() string {
return pc.name
}
func (pc PhoneConnecter) connect() {
fmt.Println("Connected", pc.name)
}
func main() {
pc := PhoneConnecter{"PhoneConnecter"}
var a connecter
a = PhoneConnecter(pc)
a.connect()
//DisConnect(a)
//Jude(a)
var y interface{}
fmt.Print(y)
var p *int = nil
y = p
fmt.Println(y == nil)
}
func DisConnect(usb USB) {
if pc, ok := usb.(PhoneConnecter); ok {
fmt.Println("This is a PhoneConnecter", pc.name)
return
}
fmt.Println("Unknown device")
}
func Jude(usb USB) {
switch v := usb.(type) {
case PhoneConnecter:
fmt.Println("Jude from PhoneConnecter", v.name)
default:
fmt.Println("Unknown device.")
}
}
有疑问加站长微信联系(非本文作者)