Skip to content

Instantly share code, notes, and snippets.

@tshrkmd
Last active February 14, 2017 01:01
Show Gist options
  • Save tshrkmd/8c676d9f5d3a27abec94fc306b614650 to your computer and use it in GitHub Desktop.
Save tshrkmd/8c676d9f5d3a27abec94fc306b614650 to your computer and use it in GitHub Desktop.
interface studty
package main
import "fmt"
type GPS struct {
lat string
lon string
}
type Wifi struct {
ssid string
channel string
}
type Bluetooth struct {
ssid string
mejar string
miner string
}
func (p *GPS) Location() string {
return p.lat + " " + p.lon
}
func (p *Wifi) Location() string {
return p.ssid + " " + p.channel
}
func (p *Bluetooth) Location() string {
return p.ssid + " " + p.mejar + " " + p.miner
}
type Location interface {
GetLocation() string
}
func printLocation(location GetLocation) {
fmt.Println(location.Location())
}
func main() {
gps := &GPS{"1111111", "111111"}
printLocation(gps)
wifi := &Wifi{"aaaaaaa", "aaaaaaa"}
printLocation(wifi)
bluetooth := &Bluetooth{"bbbbbbb", "bbbbbbb", "bbbbbbb"}
printLocation(bluetooth)
location, ok := interface{}(gps).(GetLocation)
if ok {
printLocation(location)
} else {
fmt.Println("GPS is not GetLocation intreface")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment