Last active
April 4, 2017 02:20
-
-
Save israelb/4b6dac4b54d57b3114a99e9eb34ad133 to your computer and use it in GitHub Desktop.
golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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