Why do wait only the first goroutine with
func Sleep(d Duration)
http://golang.org/pkg/time
"Sleep pauses the current goroutine for the duration d."
but the rest is execute directly. I think cause the channel c chan string
but i dont understand the behaviour between both.
All your go routines are running concurrently so they all sleep for 4 seconds at the same time, hence they all end at the same time.
You called them like this. The go
means that all the getHostName
functions run at once.
for i := 0; i < max; i++ {
go getHostName(haveHost, ipadresse_3, i)
}
And this means that all the sleeps happen at the same time
func getHostName(h chan string, ipAdresse string, n int) {
//Here!!!
time.Sleep(4 * time.Second)
ip := ipAdresse + strconv.Itoa(n)
//ip, ok := net.LookupAddr(ip)
if false {
h <- ip + " - "
} else {
h <- "error" + strconv.Itoa(n)
}
}
So they all finish at the same time.