这是完整的代码,与您在 git repo 页面上找到的代码没有太大区别。package mainimport ( "fmt" "github.com/go-ping" "time")var stats = [][]string{nil}func pinging(domain string, interval int, unit string, exit int) {current_time:= time.Now().Local()current_time.Format("02-01-2000")switch unit {case "ms": interval *= 1case "sec": interval *= 1000case "min": interval *= 6000}pinger, err := ping.NewPinger(domain)if err != nil { panic(err)}// interval between pingpinger.Interval=time.Millisecond*time.Duration(interval)//number of total pingspinger.Count=exitpinger.OnRecv = func(pkt *ping.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)}pinger.OnFinish = func(stats *ping.Statistics) { fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr) fmt.Printf("%d packets transmitted, %d packets received, %v%% packet loss\n", stats.PacketsSent, stats.PacketsRecv, stats.PacketLoss) fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n", stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)}fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())pinger.Run()}我需要将这些变量 pkt.Nbytes、pkt.IPAddr、pkt.Seq、pkt.Rtt 转换并附加到一段字符串中,这些变量是来自此 repo https://github.com/sparrc/go-ping的 *net 类型.我需要这样做,因为之后我会将所有内容打印到 .csv 我该怎么做?
1 回答
qq_笑_17
TA贡献1818条经验 获得超7个赞
将fmt.Sprintf与您的示例代码一起使用:
var s []string
s = append(s, fmt.Sprintf("%d", pkt.Nbytes)
s = append(s, fmt.Sprintf("%s", pkt.IPAddr)
s = append(s, fmt.Sprintf("%d", pkt.Seq)
s = append(s, fmt.Sprintf("%v", pkt.Rtt)
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消