Giter Club home page Giter Club logo

shades-of-golang's Introduction

Shades of Go

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.

alt

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.

Simplest

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) for cap
  • cannot use nil as type string in assignment
  • invalid operation: s == nil (mismatched types string and nil)
  • cannot assign to s[0]
  • mtx.Unlock undefined (type *** has no field or method Unlock)
  • cannot call pointer method on ***
  • syntax error: unexpected ++
  • fatal error: all goroutines are asleep - deadlock!

Intermediate

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

Advanced

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

shades-of-golang's People

Contributors

lkumarjain avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.