Created
April 7, 2017 22:58
-
-
Save israelb/bced55f81ead053c0c18d21bc2db72a7 to your computer and use it in GitHub Desktop.
Gorotines
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
// chat/httpservice/main.go | |
package httpService | |
import ( | |
"fmt" | |
"time" | |
) | |
func SendNotification() { | |
time.Sleep(5 * time.Second) | |
fmt.Println("Notification sent") | |
} |
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
// chat/lib/main.go | |
package lib | |
import ( | |
"chat/httpService" | |
"sync" | |
"time" | |
) | |
type guestConnection struct { | |
ip string | |
userName string | |
isAdmin bool | |
} | |
type visitorConnection struct { | |
ip string | |
connHour int | |
} | |
type notifier interface { | |
Notify(wg *sync.WaitGroup) | |
} | |
func (g guestConnection) Notify(wg *sync.WaitGroup) { | |
httpService.SendNotification() | |
wg.Done() | |
} | |
func (v visitorConnection) Notify(wg *sync.WaitGroup) { | |
httpService.SendNotification() | |
wg.Done() | |
} | |
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
//chat/main.go | |
package main | |
import ( | |
"chat/lib" | |
"sync" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
connections := lib.GetAllConnections() | |
connectionCount := len(connections) | |
wg.Add(connectionCount) | |
for _, c := range connections { | |
go c.Notify(&wg) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment