Skip to content

Commit

Permalink
updated eventing code guidelines (#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfaizanse authored Mar 12, 2024
1 parent 95d15a2 commit d9ac8fc
Showing 1 changed file with 0 additions and 16 deletions.
16 changes: 0 additions & 16 deletions concepts/eventing-code-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -825,22 +825,6 @@ func TestTwoDimensions(t *testing.T) {
}
```
<details>
<summary>Reason for variable reassignment (tc := tc)</summary>
The loop iteration variable in Go is a single variable. The closure for the second t.Run is executed inside a goroutine.
If there are multiple entries in the `cloudEvents` struct, `ce` references the last entry in `cloudEvents` in every iteration. The problem occurs because the closure referring to `tc` and `ce` is not executed in **sync** with the **for loop** (because of t.parallel).
To prevent this problem, `ce` and `tc` must be copied (`ce := ce`).
The linter [scopelint](https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/scopelint.go) warns about the possible problem whenever `ce` or `tc` is used.
>**CAUTION:** If you add `// nolint:scopelint` to silence scopelint, you might not notice when `ce` or `tc` references the wrong entry in the test case list.

**See Also**:
- [Go Wiki - Common Mistakes](https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables)
- [Example when using t.Parallel and for loops in table-driven tests](https://gist.github.com/posener/92a55c4cd441fc5e5e85f27bca008721)

</details>

<details>
<summary>Reason for using nested t.Run</summary>
Expand Down

0 comments on commit d9ac8fc

Please sign in to comment.