- Code not run on a VM (i.e. directly compiled to machine code).
- Automatic garbage collection.
- Collection of packages versioned together (e.g. a simple go repo on Github).
- Defined by a
go.modfile placed at its root directory.
- Directory containing on or more
.gosource files.
$ 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
From A Tour of Go:
-
Requirements for executables: a package named
mainand a function also namedmain(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"