[Go] Concurrent ping based on co-process (2)

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

The ping is encapsulated as a co-function directly by the target IP concurrency

Go itself does not provide a way to wait for a join, it needs to implement its own

/* Simple coprocessor version */package mainimport ("ping" "FMT" "Time"//"OS" "StrConv") var fin chan string; Used to implement Jionall func Pingr (host string,timeout int) {t1:=time. Now (). Unixnano () alive,err:=ping. Ping (host,timeout)//alive,err:=true,0t2:=time. Now (). Unixnano () fmt. Println (Host,alive, (T2-T1)/1000000,err);  Fin <-host}var iplist [253]stringfunc Main () {fin = make (Chan string)  //array assignment 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")}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.