Is there a linter/helper which warns about non-deterministic behaviour in Go programs? My aim is to have reproducible output in a tool used for reproducible builds. I would like the linter to warn about use of random number generators, use of time.Now()
, loops which iterate over maps and select statements, etc.
Asked
Active
Viewed 176 times
1

jochen
- 3,728
- 2
- 39
- 49
-
2note that the go compiler already supports binary-reproducibility, that is generating the exact same output file (binary) if the same source code is provided. But that is not strictly related to Determinism – Cookie04 Dec 20 '21 at 17:51
-
@Cookie04 Yes, I agree. My aim is to get the same property for my own program, which is written in Go. – jochen Dec 20 '21 at 18:09
-
1I have never heard of a linter that does something like this. Isn't it easier in this case to mock the output writer of your command(so it can write to a bytes.buffer instead of os.Stdout), run your command a few times in a test and make sure the output is always the same? This catches most of the issues you describe(accept when using dates which I guess you will not be using in your output on purpose) – Dylan Reimerink Dec 20 '21 at 18:10
-
@caveman Yes, that's what I did. But it would be nice to have some reassurance that I caught "all" of the problems, rather than "most". – jochen Dec 20 '21 at 18:12
-
Note that some actions (e.g. rangeing over a map or select) operate non-deterministic by _design_. If you need deterministic behavior Go is the wrong language. – Volker Dec 20 '21 at 21:49