github-vet / github-vet/rangeloop-pointer-findings

mf-sakura/golang_study: goroutine/wrong_sleep_sort/main.go; 15 LoC

Open
#14,880 0 comments 0 reactions 0 assignees View on GitHub
fresh small
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [mf-sakura/golang_study](https://www.github.com/mf-sakura/golang_study) at [goroutine/wrong_sleep_sort/main.go](https://github.com/mf-sakura/golang_study/blob/38b60e9b683c767ead761eb4b9f7371ed389b5ba/goroutine/wrong_sleep_sort/main.go#L19-L33)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable num used in defer or goroutine at line 25

[Click here to see the code in its original context.](https://github.com/mf-sakura/golang_study/blob/38b60e9b683c767ead761eb4b9f7371ed389b5ba/goroutine/wrong_sleep_sort/main.go#L19-L33)

Click here to show the 15 line(s) of Go which triggered the analyzer.

```go
for _, num := range nums {
// goroutineでは無名関数も使える
// numをgoroutineに渡さない悪い例
// numの値がforが進むにつれて変わるので、上手くソートが出来ない。
go func() {
// n秒スリープする
time.Sleep(time.Duration(num) * time.Second)
// mutexのLock
// 他のGoroutineからのアクセスがブロックされる
mu.Lock()
// deferで必ずUnlockする
defer mu.Unlock()
sortedNums = append(sortedNums, num)
}()
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 38b60e9b683c767ead761eb4b9f7371ed389b5ba

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.