Closed si74 closed 3 years ago
Yup running staticcheck ./... returned an error message for cases of checking var == false. I would suggest downloading the tool to run the make commands locally (https://staticcheck.io/). On a personal note, this is also a pattern I almost never see in all the Go code I've written. Typically one just does a !varName for booleans.
On Sat, Jun 5, 2021, 22:45 Nick Bouliane @.***> wrote:
@.**** commented on this pull request.
In arp.go https://github.com/netvfy/go-netvfy-agent/pull/14#discussion_r646063445:
@@ -62,7 +61,7 @@ func (t *ArpTable) Add(IP string) error {
// Check if an entry already exist before adding one.
_, ok := t.ArpMap.Load(IP)
- if ok == false {
you mean staticcheck ? can you show me what it says 🤔 is there a reference what is idiomatic go ?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/netvfy/go-netvfy-agent/pull/14#discussion_r646063445, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAG65C5GY2VY2C5GMK3CTPLTRLONHANCNFSM46E43NUQ .
@nicboul, Idecided to do a bit more research into idiomatic Go documentation and there is this -> https://golang.org/doc/effective_go#blank
I noticed, however, that this doesn't contain things that I've come to write/use very frequently over the last few years. A few such examples:
var _ TestInterface = (*MyType)(nil)
val := make([]int, 0, 5)
is preferable when possible (otherwise the underlying array needs to be resized as the slice is appended to) ok
as a boolean variable name when checking to see if a map contains a key. val, ok := mapA["keyA"]
Some of these are more along the line of style whereas others are optimizations (that the compiler would not otherwise do) and ways to force compile-time checks vs. runtime checks.
So I'd say that the best approach might be to: 1 - Check what the effective Go style guide says 2 - Rely on govet, staticcheck, and gofmt 3 - Consider other idiomatic Goisms from experience
@nicboul fell down a rabbithole but I did actually find some more links re: golang style (super helpful for me to take a look at too in the future - curious how much of this is already integrated into staticcheck):
Note this PR has three key components: