直接按目标ip并发的方式,将ping封装为协程函数
go本身没有提供等待协程join的方法,需要自己实现
/*简单协程版*/
package main
import (
"ping"
"fmt"
"time"
//"os"
"strconv"
)
var fin chan string; //用于实现jionall
func pingr(host string,timeout int){
t1:=time.Now().UnixNano()
alive,err:=ping.Ping(host,timeout)
//alive,err:=true,0
t2:=time.Now().UnixNano()
fmt.Println(host,alive,(t2-t1)/1000000,err);
fin <- host
}
var iplist [253]string
func main() {
fin = make(chan string)
//数组赋值
for i := 0; i < len(iplist); i++ {
iplist[i]="192.168.99."+strconv.Itoa(i+1)
go pingr(iplist[i],1000)
}
for i := 0; i < len(iplist); i++ {
<-fin
//fmt.Println(ip,"joined")
}
}