Created
August 25, 2014 10:20
-
-
Save Imater/fe18e0e571aeb797eec1 to your computer and use it in GitHub Desktop.
Parallel search each user in go
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
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