Home

Go Cheat Sheet

References

  1. Golang Cheat Sheet
  2. Go by example

In a nutshell

  • Imperative language
  • Statically typed
  • Syntax tokens similar to C (but less parentheses and no semicolons) and the structure to Oberon-2
  • Compiles to native code (no JVM)
  • No classes, but structs with methods
  • Interfaces
  • No implementation inheritance. There's type embedding, though.
  • Functions are first class citizens
  • Functions can return multiple values
  • Has closures
  • Pointers, but not pointer arithmetic
  • Built-in concurrency primitives: Goroutines and Channels

Basics

// hello.go package main import "fmt" func main() { fmt.Println("Hello Go") }

go run hello.go

Declarations

var foo int // declaration without initialization var foo int = 42 // declaration with initialization var foo, bar int = 42, 1302 // declare and init multiple vars at once var foo = 42 // type omitted, will be inferred foo := 42 // shorthand, only in func bodies, omit var keyword, type is always implicit const constant = "This is a constant"

Functions

// a simple function func functionName() {} // function with parameters (again, types go after identifiers) func functionName(param1 string, param2 int) {} // multiple parameters of the same type func functionName(param1, param2 int) {} // return type declaration func functionName() int { return 42 } // Can return multiple values at once func returnMulti() (int, string) { return 42, "foobar" } var x, str = returnMulti() // Return multiple named results simply by return func returnMulti2() (n int, s string) { n = 42 s = "foobar" // n and s will be returned return } var x, str = returnMulti2()

Funcs as values and closures

func main() { // assign a function to a name add := func(a, b int) int { return a + b } // use the name to call the function fmt.Println(add(3, 4)) } // Closures, lexically scoped: Functions can access values that were // in scope when defining the function func scope() func() int { outer_var := 2 foo := func() int { return outer_var} return foo } // Closures func outer() (func() int, int) { outer_var := 2 inner := func() int { outer_var += 99 // outer_var from outer scope is mutated. return outer_var } inner() return inner, outer_var // return inner func and mutated outer_var 101 }

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/go/cheat-sheet

Sections


Related