Created
October 14, 2018 11:35
-
-
Save Pushplaybang/f17044fcf23bce6ba1050a0d45ebacab to your computer and use it in GitHub Desktop.
very simple example of concurrency 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" | |
"net/http" | |
) | |
func main() { | |
links := []string{ | |
"http://golang.org", | |
"http://google.com", | |
"http://stackoverflow.com", | |
"http://facebook.com", | |
"http://amazon.com", | |
"http://netflix.com", | |
} | |
c := make(chan string) | |
for _, link := range links { | |
go checkLink(link, c) | |
} | |
for i := 0; i < len(links); i++ { | |
fmt.Println(<- c) | |
} | |
} | |
func checkLink(link string, c chan string) { | |
_, err := http.Get(link) | |
if err != nil { | |
fmt.Println(link, "seems to be down.") | |
c <- "might be down I think" | |
return | |
} | |
fmt.Println(link, "looks good.") | |
c <- "yup, thats good." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment