package main

import (
	"fmt"
	"gitee.com/liumou_site/gns"
	"gitee.com/liumou_site/logger"
	"os/exec"
	"sync"
	"time"
)

var wg sync.WaitGroup // 定义一个计数器

func ping(host string) {
	//logger.Debug("开始测试 ", host)
	r := exec.Command("ping", "-w", "1", host)
	err := r.Run()
	if err != nil {
		logger.Error("失败: ", host)
	} else {
		logger.Info("成功: ", host)
	}
	wg.Done() // 完成一个计时器
}
func main() {
	StartTime := time.Now()
	wg.Add(103) // 添加103个任务
	go ping("baidu.com")
	go ping("qq.com")
	go ping("z.liumou.site")
	IpSub, err := gns.IpGenerateList("10.1.1", 1, 100) // 生成100个IP(切片)
	if err == nil {
		for _, ip := range IpSub {
			go ping(ip)
		}
	}
	wg.Wait() // 等待所有任务完成
	UseTime := time.Since(StartTime)
	fmt.Println("耗时: ", UseTime, " 秒")
}

效果

[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.10
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  qq.com
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.1
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.9 
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.14
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.13
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  baidu.com
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.25
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.21
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.22
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.18
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.16
[2023-01-04 00:45:27] [INFO] [input.go:21] 成功:  10.1.1.19
[2023-01-04 00:45:28] [INFO] [input.go:21] 成功:  10.1.1.27
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.20
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.100
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.72
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.4
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.30
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.53
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  z.liumou.site
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.2
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.8
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.46
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.31
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.11
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.64
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.85
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.42
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.29
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.35
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.55
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.65
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.44
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.57
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.5
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.39
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.51
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.34
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.78
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.83
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.81
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.90
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.66
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.97
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.40
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.47
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.71
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.32
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.15
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.43
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.54
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.94
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.3
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.89
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.67
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.58
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.93
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.6
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.91
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.28
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.50
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.23
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.37
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.77
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.82
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.17
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.56
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.76
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.63
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.26
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.45
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.99
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.87
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.95
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.52
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.49
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.12
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.48
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.7
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.24
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.96
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.80
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.61
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.84
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.74
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.68
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.92
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.70
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.75
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.36
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.59
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.69
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.88
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.62
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.38
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.41
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.79
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.73
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.98
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.33
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.60
[2023-01-04 00:45:29] [EROR] [input.go:19] 失败:  10.1.1.86
耗时:  5.0723724s  秒