[英]How can I convert and append to a slice of strings in Golang a net type variable from go-ping repository?
This is the entire code, not much different from the one you can find on the git repo page. 这是完整的代码,与git repo页面上的代码没有太大不同。
package main
import (
"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 *= 1
case "sec":
interval *= 1000
case "min":
interval *= 6000
}
pinger, err := ping.NewPinger(domain)
if err != nil {
panic(err)
}
// interval between ping
pinger.Interval=time.Millisecond*time.Duration(interval)
//number of total pings
pinger.Count=exit
pinger.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()
} }
I need to convert and append to a slice of string these variables pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, which are type *net from this repo https://github.com/sparrc/go-ping . 我需要将这些变量pkt.Nbytes,pkt.IPAddr,pkt.Seq,pkt.Rtt转换并附加到字符串的切片上,这些变量来自此仓库https://github.com/sparrc/go-ping 。
I need to do so because after that I'll print everything to a .csv How could I do that? 我需要这样做,因为在那之后我会将所有内容打印到.csv中,我该怎么做?
Use fmt.Sprintf with your example code: 将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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.