Skip to content

Instantly share code, notes, and snippets.

@codegoalie
Created June 14, 2014 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codegoalie/a59c50329ab7c87033c2 to your computer and use it in GitHub Desktop.
Save codegoalie/a59c50329ab7c87033c2 to your computer and use it in GitHub Desktop.
Go Concurrency Examples
package main
import (
"fmt"
)
func decimal_divide(numerator, denominator int, c chan int) {
// do long division "by hand"
for ; numerator != 0; numerator = numerator % denominator {
numerator = numerator * 10
// send the next calculated digit back to main
c <- numerator / denominator
}
// all done; close the channel
close(c)
}
func main() {
numerator := 7
denominator := 8
// print the integer part of the result
fmt.Print(numerator / denominator, ".")
// calculate the decimal part digit by digit
c := make(chan int)
go decimal_divide(numerator, denominator, c)
for digit := range c {
fmt.Print(digit)
}
fmt.Print("\n")
}
package main
import (
"fmt"
)
func main() {
startingCity := "Cleveland";
// keep track of cities we've visited to avoid infinite loops
fetchedCities := map[string]bool{startingCity: true}
// pass the neighbors after fetching
c := make(chan []string)
var fetchNeighbors = func(c chan []string, city string, list cityList) {
if neighbors, ok := list[city]; ok {
// if we've found the city on the map, send back the neighbors
c <- neighbors;
} else {
// otherwise send no neighbors back
// could be a good place for an error message, etc.
c <- []string{};
}
}
// track how many cities' neighbors are still to be fetched
// start at one because of the starting city
stillToFetch := 1;
// go fetch the first set of neighbors
go fetchNeighbors(c, startingCity, cityMap);
// while we are still fetching neighbors
for stillToFetch > 0 {
// read the neighbors from the channel
neighbors := <- c
// track that we've fetched a set of neighbors
stillToFetch--;
for _, city := range neighbors {
// if we haven't seen this city before
if !fetchedCities[city] {
// mark it as seen
fetchedCities[city] = true
// now we have one more city to fetch
stillToFetch ++
// fetch
go fetchNeighbors(c, city, cityMap);
// print out the name of the neighbor as visitable
fmt.Println(city);
}
}
}
}
type cityList map[string][]string
var cityMap = cityList {
"Cleveland":
[]string{"Columbus",
"Toledo",
"Pittsburg"},
"Columbus":
[]string{"Cincinati",
"Cleveland",
"Pittsburg",
"Indinapolis"},
"Toledo":
[]string{"Detroit",
"Columbus"},
"London":
[]string{"Bristol",
"Sheffield"},
"Sheffield":
[]string{"London",
"Bristol",
"Liverpool"}}
package main
import (
"fmt"
)
func summer(start int, end int, c chan int) {
sum := 0
for i := start; i <= end; i++ {
sum += i
}
c <- sum
}
func main() {
c := make(chan int)
go summer(1, 5, c)
go summer(6, 10, c)
// read the values from the channel
first_half := <- c
last_half := <- c
fmt.Println(first_half, last_half, first_half + last_half)
}
package main
import (
"fmt"
"time"
)
func timer() {
for i := 0; i < 10; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(i)
}
}
func main() {
go timer()
fmt.Println("Timer started")
// wait for timer to complete
time.Sleep(2000 * time.Millisecond)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment