Last active
March 1, 2016 13:43
-
-
Save zemirco/26930d4681ef9a756e11 to your computer and use it in GitHub Desktop.
This file contains 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
func get(w http.ResponseWriter, r *http.Request) *HTTPError { | |
// get some logged in user | |
user := GetUserFromContext(r) | |
// create error and result channel | |
errchan := make(chan error, 2) | |
friendsChan := make(chan []Friend) | |
CarsChan := make(chan []Cars) | |
// get friends | |
go func() { | |
friends, err := db.GetFriends(user.Name) | |
if err != nil { | |
errchan <- err | |
return | |
} | |
friendsChan <- friends | |
}() | |
// get cars | |
go func() { | |
cars, err := db.GetCars(user.Name) | |
if err != nil { | |
errchan <- err | |
return | |
} | |
carsChan <- cars | |
}() | |
// define vars here | |
var friends []Friend | |
var cars []Car | |
// collect results from channels | |
for i:=0; i<2; i++ { | |
select { | |
case err := <- errchan: | |
return &HTTPError{err} | |
case friends = <- friendsChan: | |
case cars = <- carsChan: | |
// just assign the result from the channel so they can be used in the template | |
} | |
} | |
// use `friends` and `cars` to render html template | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment