This is a creation in Article, where the information may have evolved or changed.
PackageMainImport("Errors" "FMT" "Math/rand" "NET" "OS" "Time" "GOLANG.ORG/X/NET/ICMP" "Golang.org/x/net/ipv4")funcLookup (Hoststring) (string, error) {Addrs, err: = Net. Lookuphost (host)ifErr! =Nil{return "", err}if Len(Addrs) <1{return "", errors. New ("Unknown Host")} rd: = Rand. New (Rand. Newsource (time. Now (). Unixnano ()))returnAddrs[rd. INTN (Len(Addrs))],Nil}varData = []byte("Abcdefghijklmnopqrstuvwabcdefghi")typeReplystruct{TimeInt64Ttluint8Error Error}funcMain () {ping, err: = Run ("www.google.com",8, Data)ifErr! =Nil{FMT. PRINTLN (ERR)return}deferPing. Close () Ping. Ping(5) FMT. Println (ping. Pingcount(6))}funcMarshalmsg (reqint, data []byte) ([]byte, error) {XID, xseq: = OS. Getpid ()&0XFFFF, req WM: = ICMP. message{Type:ipv4. Icmptypeecho, Code:0, Body: &icmp. echo{Id:xid, Seq:xseq, Data:data,},}returnWm. Marshal (Nil)}typePingstruct{ADDRstringConn Net. Conn Data []byte}func(Self *ping) Dail () (err error) {self. Conn, err = net. Dial ("IP4:ICMP", self. ADDR)ifErr! =Nil{returnERR}return Nil}func(Self *ping) Setdeadline (timeoutint) Error {returnSelf. Conn.setdeadline (time. Now (). ADD (time. Duration (Timeout) * time. Second))}func(Self *ping) Close () Error {returnSelf. Conn.close ()}func(Self *ping) Ping (Countint) {ifERR: = self. Dail (); Err! =Nil{FMT. Println ("Not found remote host")return} FMT. Println ("Start ping from", self. CONN.LOCALADDR ()) self. Setdeadline(Ten) forI: =0; I < count; i++ {r: = Sendpingmsg (self. Conn, self. Data)ifR.error! =Nil{ifOpt, OK: = R.error. (*net. OPERROR); OK && opt. Timeout () {fmt. Printf ("from%s reply:timeout\n", self. ADDR)ifERR: = self. Dail (); Err! =Nil{FMT. Println ("Not found remote host")return} }Else{FMT. Printf ("from%s reply:%s\n", self. ADDR, R.error)}}Else{FMT. Printf ("from%s reply:time=%d ttl=%d\n", self. Addr, R.time, R.ttl)} time. Sleep(1e9) }}func(Self *ping) Pingcount (Countint) (reply []reply) {ifERR: = self. Dail (); Err! =Nil{FMT. Println ("Not found remote host")return} self. Setdeadline(Ten) forI: =0; I < count; i++ {r: = Sendpingmsg (self. Conn, self. Data) Reply =Append(reply, R) time. Sleep(1e9) }return}funcRun (addrstring, reqint, data []byte(*ping, error) {WB, err: = Marshalmsg (req, data)ifErr! =Nil{return Nil, err} addr, err = Lookup (addr)ifErr! =Nil{return Nil, err}return&PING{DATA:WB, addr:addr},Nil}funcSendpingmsg (c net. Conn, WB []byte) (reply reply) {start: = time. Now ()if_, reply. Error = C.write (WB); Reply. Error! =Nil{return} RB: = Make([]byte, the)varNintN, reply. Error = C.read (RB)ifReply. Error! =Nil{return} Duration: = time. Now (). Sub (Start) TTL: =uint8(RB[8]) RB =func(b []byte) []byte{if Len(b) < -{returnb} Hdrlen: =int(b[0]&0x0f) <<2 returnB[hdrlen:]} (RB)varRM *icmp. Message rm, reply. Error = ICMP. Parsemessage(1, Rb[:n])ifReply. Error! =Nil{return}SwitchRm. Type { CaseIPv4. Icmptypeechoreply:t: =Int64(duration/time.millisecond) Reply = Reply{t, TTL,Nil} CaseIPv4. Icmptypedestinationunreachable:reply. Error = errors. New ("Destination unreachable")default: reply. Error = FMT. Errorf ("Not icmptypeechoreply%v", RM)}return}