参考代码:
package main import ( "fmt" "net" "time" ) func doTask() { time.Sleep(1 * time.Second) main() } func main() { for { conn, err := net.DialTimeout("tcp", "223.5.5.5:80",time.Second*4) if err != nil { fmt.Println("请求失败", err) doTask() }else{ fmt.Println("请求成功") } time.Sleep(3*time.Second) conn.Close() } }代码每3秒发一次tcp连接,如果连接错误,会每隔1秒重新发起连接 标签:err,示例,fmt,tcp,golang,Second,time,main From: https://www.cnblogs.com/wenzn/p/16990021.html