
386 Rozdział 13.
Używanie wątków goroutine
Wątek goroutine funkcji
a
for i := 0; i < 50; i++ {
fmt.Print(”a”)
}
Wątek goroutine funkcji
b
for i := 0; i < 50; i++ {
fmt.Print(”b”)
}
Wątek goroutine funkcji
main
go a()
go b()
fmt.Println(”Koniec funkcji main()”)
package main
import (
“fmt”
“io/ioutil”
“log”
“net/http”
“time”
)
func main() {
go responseSize(“https://example.com/”)
go responseSize(“https://golang.org/”)
go responseSize(“https://golang.org/doc”)
time.Sleep(5 * time.Second)
}
func responseSize(url string) {
fmt.Println(“Pobieranie strony”, url)
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
body, err := ...