// 定义一个缓冲池 funcworker(ports, results chanint) { for p := range ports { addresss := fmt.Sprintf("在这里写你的url:%d", p) conn, err := net.Dial("tcp", addresss) if err != nil { results <- -p continue } conn.Close() results <- p } }
funcmain() { start := time.Now() var open_ports []int var closed_ports []int ports := make(chanint, 500) results := make(chanint) for i := 1; i < cap(ports); i++ { go worker(ports, results) } gofunc() { for i := 1; i <= 1000; i++ { ports <- i } }()
for i := 1; i < 1000; i++ { ports := <-results if ports > 0 { open_ports = append(open_ports, ports) } else { closed_ports = append(closed_ports, -ports) } }