Basics

Updated: November 24, 2025

Learn the basic components that make up the Go language.

Table of Contents

Arrays

var a [5]int          // array of 5 ints
a[4] = 100
fmt.Println(len(a))   // 5

Calls

result := add(1, 2)
x, y := swap("hello", "world")

Channels

// Declaring and initializing
c := make(chan int)

// Sending a value on a channel
c <- 1

// Receiving a value from a channel
x = <-c

// Buffered channel
ch := make(chan int, 2)

Concurrency

go func() { fmt.Println("goroutine") }()

ch := make(chan int)
go func() { ch <- 1 }()
x := <-ch

select {
case msg1 := <-ch1:
    fmt.Println("Received", msg1)
case msg2 := <-ch2:
    fmt.Println("Received", msg2)
}

Constants

const Pi = 3.14159
const (
    A = iota  // 0
    B         // 1
    C         // 2
)

Conversions

var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

Errors

type error interface {
    Error() string
}

var ErrNotFound = errors.New("not found")

func doSomething() error {
    return fmt.Errorf("something went wrong: %v", err)
}

Functions

func add(x, y int) int {
    return x + y
}

func swap(x, y string) (string, string) {
    return y, x
}

func apply(f func(int, int) int, x, y int) int {
    return f(x, y)
}

Interfaces

type Reader interface {
    Read(p []byte) (n int, err error)
}

type Writer interface {
    Write(p []byte) (n int, err error)
}

var r Reader = os.Stdin

Loops

// Traditional for
for i := 0; i < 10; i++ {
    fmt.Println(i)
}

// While-like
i := 0
for i < 10 {
    fmt.Println(i)
    i++
}

// Infinite loop
for {
    // do something
}

Maps

m := make(map[string]int)
m["key"] = 42
value := m["key"]
delete(m, "key")
_, exists := m["key"]

Methods

type Vertex struct {
    X, Y float64
}

func (v Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func (v *Vertex) Scale(f float64) {
    v.X *= f
    v.Y *= f
}

Pointers

var p *int
i := 42
p = &i
fmt.Println(*p)  // 42
*p = 21          // changes i

Range

nums := []int{1, 2, 3}
for i, num := range nums {
    fmt.Println(i, num)
}

m := map[string]int{"a": 1, "b": 2}
for key, value := range m {
    fmt.Println(key, value)
}

Scope

var global = "global"  // package scope

func main() {
    local := "local"   // function scope
    {
        block := "block"  // block scope
    }
}

Slices

s := make([]int, 5)     // len=5, cap=5
s = append(s, 6)        // len=6, cap=10 (doubled)
sub := s[1:3]           // slice from index 1 to 3

Structs

type Person struct {
    Name string
    Age  int
}

type Employee struct {
    Person
    Salary int
}

p := Person{Name: "Alice", Age: 30}

Switches

switch day := "monday"; day {
case "monday", "tuesday":
    fmt.Println("Weekday")
case "saturday", "sunday":
    fmt.Println("Weekend")
default:
    fmt.Println("Invalid day")
}

// Type switch
var i interface{} = "hello"
switch v := i.(type) {
case int:
    fmt.Printf("Twice %v is %v\n", v, v*2)
case string:
    fmt.Printf("%q is %v bytes long\n", v, len(v))
}

Syntax

// Single line comment
/* Multi-line
   comment */

package main
import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Types

bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
float32 float64
complex64 complex128
byte  // alias for uint8
rune  // alias for int32

Variables

var i int = 42
var f float64 = 3.14
x := 42        // inferred as int
a, b := 1, 2   // multiple assignment

Zero Values

var i int       // 0
var f float64   // 0.0
var b bool      // false
var s string    // ""
var p *int      // nil