#### 我有一些抓取操作,需要1秒钟实现10个api的请求,我把那些数据,临时存储到数组里面,但是不知道怎么通过并发的形式实现
```go
func main() {
timeout := time.NewTicker(1 * time.Second).C
var zips = []string{"01", "02", "03", "04", "05", "06", "07", "08", "14", "15", "16", "09", "10", "11", "12", "13", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "81", "51", "52", "53", "54", "55", "82", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "77", "78", "75", "76", "79", "80"}
for {
select {
case <-timeout:
println("时间:", time.Now().Second())
for i := 0; i < 10; i++ {
println(zips[i])
}
}
}
}
```
10楼 <a href="/user/didadi" title="@didadi">@didadi</a>
大哥 这样你能理解不,我需要生成100个数据,这100个数据中的每一个 又能生成1万条数据,我需要每秒秒并发请求这1万条数据中的10个。
```
for i:=0;i<10;i++{
//这里有1万条数据
zips := make([]string, 10000)
//数据存入过程省略
//处理数据
for _, v := range zips {
tmp[l] = v
if (l == max-1) {
//loop:
for {
select {
case <-tk.C:
for i := 0; i < 50; i++ {
tmp_i := i
go run(tmp[tmp_i], info)
}
quit <- struct{}{}
//break loop
case <-quit:
// to receive error and quit
return
}
}
l = 0
tmp = make([] string, max)
tmp[l] = v
continue
}
l++
}
}
```
上面这个代码 会出现报错,数据竞争的问题?
```
==================
WARNING: DATA RACE
Write at 0x00c4200ac018 by goroutine 17:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:910 +0x40
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:158 +0x193
Previous write at 0x00c4200ac018 by goroutine 47:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:910 +0x40
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:158 +0x193
Goroutine 17 (running) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x6f6
main.main()
/Users/jack_lee/project/singapore/main.go:217 +0x178
Goroutine 47 (finished) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x6f6
main.main()
/Users/jack_lee/project/singapore/main.go:217 +0x178
==================
==================
WARNING: DATA RACE
Write at 0x00c4200ac030 by goroutine 17:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:911 +0x5e
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:158 +0x193
Previous write at 0x00c4200ac030 by goroutine 47:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:911 +0x5e
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:158 +0x193
Goroutine 17 (running) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x6f6
main.main()
/Users/jack_lee/project/singapore/main.go:217 +0x178
Goroutine 47 (finished) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x6f6
main.main()
/Users/jack_lee/project/singapore/main.go:217 +0x178
==================
^C
[21:05:42] jack_lee:singapore git:(master*) $ go build -race -o main main.go && ./main
==================
WARNING: DATA RACE
Write at 0x00c4200a8018 by goroutine 29:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:910 +0x40
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:159 +0x193
Previous write at 0x00c4200a8018 by goroutine 56:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:910 +0x40
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:159 +0x193
Goroutine 29 (running) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x5ce
main.main()
/Users/jack_lee/project/singapore/main.go:218 +0x178
Goroutine 56 (running) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x5ce
main.main()
/Users/jack_lee/project/singapore/main.go:218 +0x178
==================
==================
WARNING: DATA RACE
Write at 0x00c4200a8030 by goroutine 29:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:911 +0x5e
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:159 +0x193
Previous write at 0x00c4200a8030 by goroutine 56:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:911 +0x5e
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:159 +0x193
Goroutine 29 (running) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x5ce
main.main()
/Users/jack_lee/project/singapore/main.go:218 +0x178
Goroutine 56 (running) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x5ce
main.main()
/Users/jack_lee/project/singapore/main.go:218 +0x178
==================
==================
WARNING: DATA RACE
Write at 0x00c4200a8018 by goroutine 21:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:910 +0x40
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:159 +0x193
Previous write at 0x00c4200a8018 by goroutine 56:
flag.(*FlagSet).Parse()
/usr/local/go/src/flag/flag.go:910 +0x40
flag.Parse()
/usr/local/go/src/flag/flag.go:941 +0xbd
lubi_data/common.Println()
/Users/jack_lee/project/common/common.go:74 +0x4e
main.run()
/Users/jack_lee/project/singapore/main.go:159 +0x193
Goroutine 21 (running) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x5ce
main.main()
/Users/jack_lee/project/singapore/main.go:218 +0x178
Goroutine 56 (finished) created at:
main.gen_math()
/Users/jack_lee/project/singapore/main.go:112 +0x5ce
main.main()
/Users/jack_lee/project/singapore/main.go:218 +0x178
==================
```
#11