Skip to content

Instantly share code, notes, and snippets.

@Imater
Created August 25, 2014 10:20
Show Gist options
  • Save Imater/fe18e0e571aeb797eec1 to your computer and use it in GitHub Desktop.
Save Imater/fe18e0e571aeb797eec1 to your computer and use it in GitHub Desktop.
Parallel search each user in go
package main
import (
"fmt"
"time"
"strconv"
)
func getIdByFio (fio string) chan int {
c := make(chan int)
go func (){
time.Sleep(time.Second*1)
c <- len(fio)
}()
return c
}
func getTax (id int) chan string{
c := make(chan string)
time.Sleep(time.Second*2)
go func (){
c <- "Tax for user " + strconv.Itoa(id) + " is " + strconv.Itoa(id*2)
}()
return c
}
func main (){
userList := []string{"Wezel", "Go", "Bill Gates", "Lexus", "Boom", "Gone"}
for _, fio := range userList {
go func (fio string){
fmt.Printf("Start search tax for %v\n", fio)
id := getIdByFio(fio)
fmt.Println(<-getTax(<-id))
}(fio)
}
time.Sleep(time.Second*30)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment