Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "io/ioutil"
- "net/http"
- )
- func fetchSite(url string) (string, error) {
- resp, err := http.Get(url)
- defer resp.Body.Close()
- data, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return "", err
- }
- return string(data), nil
- }
- func fetchGoogle(q string) string {
- var result string
- var err error
- func() {
- result, err = fetchSite("https://www.google.com/search?q=" + q)
- }()
- if err != nil {
- return "Error"
- }
- return "Google"
- }
- func fetchYandex(q string) string {
- var result string
- var err error
- func() {
- result, err = fetchSite("https://yandex.ru/search/?text=" + q)
- }()
- if err != nil {
- return "Error"
- }
- return "Yandex"
- }
- func main() {
- var i int
- var yCounter int
- var gCounter int
- var tests int
- respGoogle := make(chan bool)
- respYandex := make(chan bool)
- fmt.Print("Tests to perform: ")
- fmt.Scan(&tests)
- for i=1; i <= tests; i++ {
- go func() {
- if fetchYandex("test") != ""{
- respYandex <- true
- } else {
- fmt.Println("An error occurred with Yandex")
- }
- }()
- go func() {
- if fetchGoogle("test") != ""{
- respGoogle <- true
- } else {
- fmt.Println("An error occurred with Google")
- }
- }()
- select {
- case <- respGoogle:
- gCounter += 1
- fmt.Printf("Test %d: Google was faster\n", i)
- case <- respYandex:
- yCounter += 1
- fmt.Printf("Test %d: Yandex was faster\n", i)
- }
- }
- fmt.Printf("By the results of %d tests, ", i-1)
- if gCounter > yCounter{
- fmt.Printf("Google was faster (%d vs. %d)\n", gCounter, yCounter)
- } else if yCounter > gCounter {
- fmt.Printf("Yandex was faster (%d vs. %d)\n", yCounter, gCounter)
- } else {
- fmt.Printf("Google and Yandex showed the same performance")
- }
- }
Add Comment
Please, Sign In to add comment