-7

For example, the "Effective Go" documentation has the following entries:

Like C, Go's formal grammar uses semicolons to terminate statements, but unlike in C, those semicolons do not appear in the source. Instead the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them.

Cut off some parts for brevity.

Write them like this

if i < f() {
    g()
}

not like this

if i < f()  // wrong!
{           // wrong!
    g()
}

The wrong version when executed produces the following error messages:

/tmp/test.go:6: missing condition in if statement
/tmp/test.go:6: true evaluated but not used

IMO, both messages don't give the coder a clue about misplaced curly braces. Had I failed to read the documentation above, I would have probably written some Go code in the future using the wrong version (I usually use the 1st version in writing code that uses curly braces), then banged my head as to why I'm missing an "if" condition when one is clearly present.

Are there other "gotchas" in Golang that I should be aware of?

B. Kelso
  • 57
  • 3
  • Why did you get so many downvotes? Is it because the pun is not phunny unlike [this question](https://stackoverflow.com/questions/6268768/c-gotchas-and-mistakes-for-c-programmers), which got three upvotes with a similar topic? That’s unfair of you, community. – Константин Ван Apr 26 '21 at 13:20

1 Answers1

7

This could've been just a comment, but posted as an answer to lay more emphasis on it, because I do believe the linked article is really good, useful even for non-starters and it is still unknown.


This is the best and most complete I've seen:

50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs

It has topics ranging from total beginner (like "Opening Brace Can't Be Placed on a Separate Line" or "Unused Imports") to advanced beginner (like "nil" Interfaces and "nil" Interfaces Values or "Preemptive Scheduling").

So a nice summary of the common pitfalls or "gotchas" is basically their table of contents, all of which you can read more about there:

Total Beginner:

  • Opening Brace Can't Be Placed on a Separate Line
  • Unused Variables
  • Unused Imports
  • Short Variable Declarations Can Be Used Only Inside Functions
  • Redeclaring Variables Using Short Variable Declarations
  • Accidental Variable Shadowing
  • Can't Use "nil" to Initialize a Variable Without an Explicit Type
  • Using "nil" Slices and Maps
  • Map Capacity
  • Strings Can't Be "nil"
  • Array Function Arguments
  • Unexpected Values in Slice and Array "range" Clauses
  • Slices and Arrays Are One-Dimensional
  • Accessing Non-Existing Map Keys
  • Strings Are Immutable
  • Conversions Between Strings and Byte Slices
  • Strings and Index Operator
  • Strings Are Not Always UTF8 Text
  • String Length
  • Missing Comma In Multi-Line Slice/Array/Map Literals
  • log.Fatal and log.Panic Do More Than Log
  • Built-in Data Structure Operations Are Not Synchronized
  • Iteration Values For Strings in "range" Clauses
  • Iterating Through a Map Using a "for range" Clause
  • Fallthrough Behavior in "switch" Statements
  • Increments and Decrements
  • Bitwise NOT Operator
  • Operator Precedence Differences
  • Unexported Structure Fields Are Not Encoded
  • App Exits With Active Goroutines
  • Sending to an Unbuffered Channel Returns As Soon As the Target Receiver Is Ready
  • Sending to an Closed Channel Causes a Panic
  • Using "nil" Channels
  • Methods with Value Receivers Can't Change the Original Value

Intermediate Beginner:

  • Closing HTTP Response Body
  • Closing HTTP Connections
  • Unmarshalling JSON Numbers into Interface Values
  • Comparing Structs, Arrays, Slices, and Maps
  • Recovering From a Panic
  • Updating and Referencing Item Values in Slice, Array, and Map "for range" Clauses
  • "Hidden" Data in Slices
  • Slice Data Corruption
  • "Stale" Slices
  • Type Declarations and Methods
  • Breaking Out of "for switch" and "for select" Code Blocks
  • Iteration Variables and Closures in "for" Statements
  • Deferred Function Call Argument Evaluation
  • Deferred Function Call Execution
  • Failed Type Assertions
  • Blocked Goroutines and Resource Leaks

Advanced Beginner:

  • Using Pointer Receiver Methods On Value Instances
  • Updating Map Value Fields
  • "nil" Interfaces and "nil" Interfaces Values
  • Stack and Heap Variables
  • GOMAXPROCS, Concurrency, and Parallelism
  • Read and Write Operation Reordering
  • Preemptive Scheduling
icza
  • 389,944
  • 63
  • 907
  • 827
  • Awesome, thanks! Looks daunting but a few of those will produce an explicit error message though like "Unused imports". I will go through the list to see which ones produce error messages that may seem unrelated to the actual error. The list should end up being smaller. – B. Kelso Oct 18 '15 at 06:10