Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Go 1.20 Compatibility #1035

Merged
merged 6 commits into from
Feb 21, 2023
Merged

Conversation

roger2hk
Copy link
Contributor

@roger2hk roger2hk commented Feb 13, 2023

The cloud build starts using Go 1.20. The changes below are necessary to support Go 1.20.

  • Fix deprecated go test -i flag
  • Bump golangci-lint from v1.50.1 to v1.51.1
  • Fix loopclosure: loop variable test captured by func literal
  • Remove deprecated global random number generator rand.Seed in trillian/integration

Checklist

@roger2hk roger2hk changed the title Fix deprecated go test -i flag Add Go 1.20 Compatibility Feb 14, 2023
@roger2hk roger2hk self-assigned this Feb 15, 2023
@roger2hk
Copy link
Contributor Author

@AlCutter There are staticcheck warning messages for rand.Seed(*seed) as Go 1.20 now automatically seeds the global random number generator (used by top-level functions like Float64 and Int) with a random value. There is no way to get the automatically generated seed. It is recommended to use rand.New(rand.NewSource(seed)) which returns the a new Rand.

Should we temporarily create the golangci-lint exclude rule with SA1019 and these two files? There is no way to retrieve the automatically generated seed in Go 1.20. If we use the recommended rand.New(rand.NewSource(seed)), the newly created Rand needs to be passed into all rand functions in other packages. This Go playground example shows the difference.

trillian/integration/ct_hammer/main.go:184:2: SA1019: rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: Programs that call Seed and then expect a specific sequence of results from the global random source (using functions such as Int) can be broken when a dependency changes how much it consumes from the global random source. To avoid such breakages, programs that need a specific result sequence should use NewRand(NewSource(seed)) to obtain a random generator that other packages cannot access. (staticcheck)
	rand.Seed(*seed)
	^
trillian/integration/ct_integration_test.go:56:2: SA1019: rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: Programs that call Seed and then expect a specific sequence of results from the global random source (using functions such as Int) can be broken when a dependency changes how much it consumes from the global random source. To avoid such breakages, programs that need a specific result sequence should use NewRand(NewSource(seed)) to obtain a random generator that other packages cannot access. (staticcheck)
	rand.Seed(*seed)
	^
The [math/rand](https://tip.golang.org/pkg/math/rand/) package now automatically seeds the global random number generator (used by top-level functions like Float64 and Int) with a random value, and the top-level [Seed](https://tip.golang.org/pkg/math/rand/#Seed) function has been deprecated. Programs that need a reproducible sequence of random numbers should prefer to allocate their own random source, using rand.New(rand.NewSource(seed)).

Programs that need the earlier consistent global seeding behavior can set GODEBUG=randautoseed=0 in their environment.

@roger2hk roger2hk force-pushed the fix-go-test-deprecated-arg branch from f9fa48b to 2cfb74b Compare February 20, 2023 21:22
@roger2hk roger2hk requested a review from AlCutter February 20, 2023 23:31
@roger2hk roger2hk marked this pull request as ready for review February 20, 2023 23:31
@roger2hk roger2hk requested a review from a team as a code owner February 20, 2023 23:31
@roger2hk roger2hk merged commit f944df0 into google:master Feb 21, 2023
@roger2hk roger2hk deleted the fix-go-test-deprecated-arg branch February 21, 2023 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants