Skip to content

Instantly share code, notes, and snippets.

@israelb
Created April 7, 2017 22:58
Show Gist options
  • Save israelb/bced55f81ead053c0c18d21bc2db72a7 to your computer and use it in GitHub Desktop.
Save israelb/bced55f81ead053c0c18d21bc2db72a7 to your computer and use it in GitHub Desktop.
Gorotines
// chat/httpservice/main.go
package httpService
import (
"fmt"
"time"
)
func SendNotification() {
time.Sleep(5 * time.Second)
fmt.Println("Notification sent")
}
// 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}
}
//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