You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I currently work in a team and we had a code issue a few times that made it beyond code review.
It is pre-allocating a slice with size instead of cap, then use append on it. Example
ar := make([]int, 10)
for i:=0; i<10; i++ {
ar = append(ar, i)
}
The correct pre-alloc would be ar := make([]int, 0, 10). Because of the missing 0,, the faulty code produces an array of size 20.
Would it be possible to add a check for this into your linter? I could not find any linter that has this check.
The text was updated successfully, but these errors were encountered:
Hi, thanks for reaching out. At first glance, it looks like it might be possible to add some sort of check for these types of errors. I'll try to take a closer look when I get a moment.
Thank you very much. I believe this mistake is quite common, especially due to the confusing pre-allocation of arrays vs maps in Golang. In arrays, the extra ,0 is needed to specify capacity, but not in maps.
I currently work in a team and we had a code issue a few times that made it beyond code review.
It is pre-allocating a slice with size instead of cap, then use append on it. Example
The correct pre-alloc would be
ar := make([]int, 0, 10)
. Because of the missing0,
, the faulty code produces an array of size 20.Would it be possible to add a check for this into your linter? I could not find any linter that has this check.
The text was updated successfully, but these errors were encountered: