golang 检查网络状态是否正常的方法
时间:2021-05-13 08:13:43|栏目:Golang|点击: 次
如下所示:
package main import ( "fmt" "os/exec" "time" ) func NetWorkStatus() bool { cmd := exec.Command("ping", "baidu.com", "-c", "1", "-W", "5") fmt.Println("NetWorkStatus Start:", time.Now().Unix()) err := cmd.Run() fmt.Println("NetWorkStatus End :", time.Now().Unix()) if err != nil { fmt.Println(err.Error()) return false } else { fmt.Println("Net Status , OK") } return true } func main() { fmt.Println("Hello ", NetWorkStatus()) }
运行结果:
Start: 1525266343 End: 1525266343 Net Status , OK Hello, true