Skip to content

Instantly share code, notes, and snippets.

@h4k1m0u
Last active September 21, 2025 15:06
Show Gist options
  • Save h4k1m0u/0f4802dc3a942966131cbc3dc5544e3a to your computer and use it in GitHub Desktop.
Save h4k1m0u/0f4802dc3a942966131cbc3dc5544e3a to your computer and use it in GitHub Desktop.
Notes about Go

Features

  • Code not run on a VM (i.e. directly compiled to machine code).
  • Automatic garbage collection.

Module

  • Collection of packages versioned together (e.g. a simple go repo on Github).
  • Defined by a go.mod file placed at its root directory.

Package

  • Directory containing on or more .go source files.

Commands

$ go mod init github.com/h4k1m0u/repo  # Initialize a new module in the current directory (ie. creates a new go.mod file)
$ go run .  # Compile and run the main go package
$ go mod tidy  # Install remote packages by scanning code for imports

Notions

From A Tour of Go:

  • Requirements for executables: a package named main and a function also named main (entry point of the program).

  • A name (fct or var) in a package is exported only if it begins with capital letter.

  • Types come after variable names.

  • A function can return any number of results.

  • It is possible to declare & initalize multiple variables on the same line.

  • Short variable declaration (:=)

    • To declare and initialize a variable: s := "hello" (valid only in a function).

    • Equivalent to: var s string; s = "hello"

  • Pointers: same principle as in C.

  • Variables without an initial value are zero-initialized.

  • Casting: var i int = 42; var f float64 = float64(i)

  • Constants declared with: const world = "monde"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment