ardanlabs / gotour

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove rand.Seed calls?

alexbozhenko opened this issue · comments

rand.Seed is deprecated and not needed anymore: https://pkg.go.dev/math/rand#Seed

Should it be removed everywhere?
https://sourcegraph.com/search?q=context:global+rand.Seed+repo:github.com/ardanlabs/gotour&patternType=standard&sm=1&groupBy=repo

Also, there is probably some linter in golangci-lint that alerts about usage of deprecated functions. Should it be enabled, to catch stuff like this automatically?

I didn't know that and I will fix the examples. I use staticcheck and I am not sure it is checking for this. I've only used this API for examples. Thanks!

Turns out, there is a check: https://staticcheck.dev/docs/checks/#SA1019

The problem is build directive excludes all the go files under _content.

[alex@omenlaptop gotour]$ staticcheck -checks SA1019  github.com/ardanlabs/gotour/_content/tour/data_race
-: build constraints exclude all Go files in /home/alex/code/gotour/_content/tour/data_race (compile)

If we drop the build directive, the warning shows up:

[alex@omenlaptop gotour]$ sed -i '1,2d' _content/tour/data_race/exercise1.go
[alex@omenlaptop gotour]$ staticcheck -checks SA1019  github.com/ardanlabs/gotour/_content/tour/data_race
_content/tour/data_race/exercise1.go:20:2: rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: As of Go 1.20 there is no reason to call Seed with a random value. Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator.  (SA1019)

I tend to use staticcheck -checks=all ./... when running staticcheck. I am not sure I have that setup with the project. I will look. This is still on my list.

I have fixed all the linter issues and removed this API.