Go is a simple and powerful language, but unlike any other language it also has few Gotchas… Some of these mistakes are natural traps for programmers, especially if you are accustomed to some other language thought process.
Gotcha is a valid construct in a system, program or programming language that works as documented but is counter-intuitive and almost invites mistakes because it is both easy to invoke and unexpected or unreasonable in its outcome. Source: Wikipedia
I am trying to list couple of Gotchas
along with alternatives
to address them, I came across while learning Golang. I would love to hear feedback
and learn from you
in case I have missed some of them and you have encountered these while development… I have divided these Gotchas in to multiple categories based on the complexity.
These are very simplest form of Gotchas as we can catch them at compile time. Compiler or linters (static analysis tools) complains about these mistakes and we can resolve them quickly. Some of them are:
- unexpected semicolon or newline before
{
- *** declared and
not used
- imported and
not used
- non-declaration statement
outside function body
- no new variables on left side of
:=
- non-name *** on left side of
:=
- use of untyped
nil
- invalid argument *** (type
map[string]int)
forcap
- cannot use
nil
as typestring
in assignment - invalid operation:
s == nil
(mismatched typesstring
andnil
) - cannot assign to
s[0]
mtx.Unlock
undefined (type *** has no field or methodUnlock
)- cannot call
pointer method
on *** - syntax error: unexpected
- fatal error: all
goroutines are asleep
- deadlock!
These are little tricky as to fix as your program behave abruptly at runtime. We can address such Gotchas with little investigation because these are directly visible at runtime in form of unexpected behavior, panic etc.
- variable-shadow
- zero-sized-variables
- entry-in-nil-map
- unexpected-values-in-range
- update-reference-item-in-range
- breaking-out-of-for-switch
- breaking-out-of-for-select
- data-race-closure-and-iteration-variable
- recovering-from-a-panic
- case-statement-is-not-executing
- empty-structs-in-the-json-text-output
- nil-is-not-equal-to-nil
- why-goroutine-execution-did-not-complete
- working-with-closed-channel
- invalid-memory-address-or-nil-pointer-dereference
- the-trim-function
These are the most complex Gotchas and needs good amount of digging to identify and address as they may not be directly visible in the program such as resource leak, blocking channel, deadlocks, routine leaks, infinite loop etc.
- iota-does-not-always-start-with-zero
- array-function-arguments
- non-existing-map-keys
- hidden-data-in-slices
- append-can-cause-data-corruption-in-slice
- unexpected-value-is-being-passed-in-differed-function
- wrong-use-of-defer-can-cause-resource-leak
- why-execution-flow-is-blocked-for-channel
- where-is-my-copy