Answer the question
In order to leave comments, you need to log in
Why is there no output of the function result to the console when using Go-routines?
As far as I understand, the main thread completes faster than the routines complete, but I don’t understand how to synchronize.
package main
import (
"fmt"
"net/http"
"time"
)
func getResp() {
res, err := http.Get("http://ya.ru")
if err != nil {
fmt.Println(err)
}
fmt.Println(res.Status)
}
func main() {
start := time.Now()
for i := 0; i < 10; i++ {
go getResp()
}
elapsed := time.Since(start)
fmt.Println("Время выполнения ", elapsed)
}
package main
import (
"fmt"
"net/http"
"time"
)
func getResp()string {
res, err := http.Get("http://ya.ru")
if err != nil {
fmt.Println(err)
}
return res.Status
}
func main() {
start := time.Now()
for i := 0; i < 10; i++ {
go func() {fmt.Println(getResp())}()
}
elapsed := time.Since(start)
fmt.Println("Время выполнения ", elapsed)
}
Answer the question
In order to leave comments, you need to log in
package main
import (
"fmt"
"net/http"
"sync"
"time"
)
var wg sync.WaitGroup
func getResp() {
defer wg.Done()
res, err := http.Get("http://ya.ru")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(res.Status)
}
}
func main() {
start := time.Now()
for i := 0; i < 10; i++ {
wg.Add(1)
go getResp()
}
wg.Wait()
elapsed := time.Since(start)
fmt.Println("Время выполнения ", elapsed)
}
Thanks Andrey Kot , I got it more creepy
package main
import (
"fmt"
"net/http"
"sync"
"time"
)
func getResp() string {
res, err := http.Get("http://ya.ru")
if err != nil {
fmt.Println(err)
time.Sleep(1)
}
return res.Status
}
func main() {
start := time.Now()
countThread := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
countThread.Add(1)
go func(cT *sync.WaitGroup) {
fmt.Println(getResp())
cT.Done()
}(countThread)
}
countThread.Wait()
elapsed := time.Since(start)
fmt.Println("Время выполнения ", elapsed)
}
The router's DHCP IP may be expiring.
Try to set a week, for a home network this is not critical.
Or on a Mac, set the static IP network settings
yes, a static IP for each MAC address in LAN, set manually (not auto) the Wi-Fi channel least clogged with neighbors, and naturally change to 8-digit new passwords for the waffle and the route's webmord itself (otherwise, maybe a bunch of neighbors got into you there, and it’s such a waffle, it gives priority to newly connected ones),
but the main thing I think is a bodyaga with a power supply for the route - do it like me, power the route through UPS (at least it’s because of the shnyaga of 220 that the route was constantly stupid. now everything is exactly already with year)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question