Skip to content

Instantly share code, notes, and snippets.

@israelb
Last active April 4, 2017 02:20
Show Gist options
  • Save israelb/4b6dac4b54d57b3114a99e9eb34ad133 to your computer and use it in GitHub Desktop.
Save israelb/4b6dac4b54d57b3114a99e9eb34ad133 to your computer and use it in GitHub Desktop.
golang
package main
import (
"fmt"
"time"
)
type guestConnection struct {
ip string
userName string
isAdmin bool
}
type visitorConnection struct {
ip string
connHour int
}
type notifier interface{
notify()
}
// one implementation for guestConnection
func (g guestConnection) notify() {
fmt.Println("Guest connection from user name:", g.userName)
}
// and a different implementation for visitorConnection
func ( v visitorConnection ) notify() {
fmt.Println("Visitor connected at:", v.connHour)
}
func main() {
notifiers := getAllConnections()
for _, c := range notifiers {
c.notify()
}
}
func getAllConnections() []notifier {
gConn := &guestConnection{ip: "192.168.0.10", userName: "Darth Vader"}
vConn := &visitorConnection{ip: "192.168.0.11", connHour: time.Now().Hour()}
return []notifier{gConn, vConn}
}
package main
import "fmt"
type guestConnection struct {
ip string
userName string
isAdmin bool
}
func (g guestConnection) notify() {
fmt.Println("Guest connection from user name:", g.userName)
}
func main() {
guestConns := getAllConnections()
for _, c := range guestConns {
c.notify()
}
}
func getAllConnections() []*guestConnection {
gConn1 := &guestConnection{ip: "192.168.0.10", userName: "Darth Vader"}
gConn2 := &guestConnection{ip: "192.168.0.11", userName: "Obi-Wan"}
return []*guestConnection{gConn1, gConn2}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment