Skip to content

Instantly share code, notes, and snippets.

@spddl
Created December 16, 2019 19:20
Show Gist options
  • Save spddl/1084b1bf47f65dde1d987772a92db0be to your computer and use it in GitHub Desktop.
Save spddl/1084b1bf47f65dde1d987772a92db0be to your computer and use it in GitHub Desktop.
// https://pragmacoders.com/blog/multithreading-in-go-a-tutorial
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
query := "Our Query"
respond := make(chan string)
go googleIt(respond, query)
queryResp := <-respond
fmt.Printf("Sent query:\t\t %s\n", query)
fmt.Printf("Got Response:\t\t %s\n", queryResp)
}
func googleIt(respond chan<- string, query string) {
time.Sleep(time.Duration(rand.Intn(10)) * time.Second)
respond <- "A Google Response"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment