Go fundamentals
Golang fundamentals
Basics
Program structure
Every Go source file starts with a package declaration, followed by imports, then
top-level declarations. A runnable program uses package main and a func main()
entry point.
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("Hiii")
fmt.Println("Here is a random number:", rand.Intn(10))
}Running and building
go run compiles and runs in one step, without leaving a binary behind.
go build compiles and writes an executable you can run yourself.
go run main.go # compile + run, no binary kept
go build # produces ./<module-name>
./<module-name> # run the built binaryProject types and modules
A Go project is one of two kinds:
- Executable: has
package mainand afunc main()entry point. - Package: a reusable library, imported by other code.
go.mod
go.mod is the root definition file for dependency management. A module is a collection of packages, and its name is usually the path where the code is hosted.
module github.com/yourname/yourproject
go 1.23.0
require (
github.com/gorilla/mux v1.8.1
github.com/lib/pq v1.10.9
)
replace github.com/gorilla/mux => ../local-mux-forkmodule: the module path, usually the code hosting URL.go: the minimum language version required to compile the code.require: dependencies with their versions.replace: swaps a dependency for a local path or fork.
Real-world example to study: fider's go.mod
Functions
Parameters are typed per name. When several consecutive parameters share a type, you can list the names and type once.
func add(x int, y int) int {
return x + y
}
func add2(x, y int) int { // x and y are both int
return x + y
}Functions can return multiple values.
func swap(x string, y string) (string, string) {
return y, x
}
a, b := swap("hello", "world") // a = "world", b = "hello"Return values can be named. The names are treated like variables declared at
the top of the function, and a bare return returns them.
func add3(x int, y int) (result int) {
result = x + y
return // returns result
}Variables
var declares one variable, or a block of them. A declared variable can omit its
type when an initializer is present (the type is inferred).
var i int = 42
var f float64 = float64(i) // explicit conversion
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1 // largest uint64
z complex128 = cmplx.Sqrt(-5 + 12i)
)Inside a function, := is a short form that declares and infers the type.
It requires at least one new variable on the left.
i, j := 42, 2701 // both intZero values
Every declared-but-not-initialized variable gets a zero value based on its type:
0 for numbers, false for bools, "" for strings, nil for pointers, slices,
maps, channels, and interfaces.
Basic types
| Type | Notes |
|---|---|
bool |
true / false |
string |
UTF-8 text |
int, int8…int64 |
signed integers; int is platform word size |
uint, uint8…uint64, uintptr |
unsigned integers |
float32, float64 |
floating point |
complex64, complex128 |
complex numbers |
Go is strictly typed: there is no implicit conversion between numeric types.
var i int = 42
var f float64 = float64(i) // int -> float64 needs an explicit conversion
var u uint = uint(f) // float64 -> uint needs an explicit conversionPrinting
fmt.Println prints values separated by spaces and adds a newline. fmt.Printf
formats using verbs: %v prints the value, %T prints its type.
fmt.Printf("Type: %T Value: %v\n", i, i)
// Type: int Value: 42