JSTAcademy
0 XP
Dashboard
Crash Courses
Variables, Types & Pointers
25 min
Basic+160 XP
Crash Courses · Basic

Variables, Types & Pointers

Static typing, zero values, and direct memory addressing
25 min read+160 XP on completionCert: Go Crash Course
Tap any word in the text below to start reading from there.

Variables, Types, and Pointers

Declaring Variables

Go is statically typed — every variable has a fixed type determined at compile time.

package main

import "fmt"

func main() {
    // Long form: explicit type
    var name string = "Jordan"
    var age  int    = 28

    // Short form: type inferred (only inside functions)
    city   := "Kingston"
    score  := 98.5 // float64

    fmt.Println(name, age, city, score)
}

Built-in Types

CategoryTypes
Integerint, int8, int16, int32, int64, uint, uint8 …
Floatfloat32, float64
Stringstring (immutable UTF-8 bytes)
Boolbool
Bytebyte (alias for uint8)
Runerune (alias for int32, represents a Unicode code point)

Zero Values

Every Go variable is automatically initialized:

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

This eliminates a huge class of "uninitialized variable" bugs common in C/C++.

Constants

const Pi      = 3.14159
const MaxSize = 1024
const Greeting = "Hello"

Pointers

A pointer stores a memory address. Use & to get an address and * to read/write through it.

package main

import "fmt"

func increment(n *int) {
    *n = *n + 1 // dereference and modify
}

func main() {
    x := 10
    fmt.Println("before:", x) // 10

    increment(&x)              // pass address of x
    fmt.Println("after:", x)  // 11

    // Pointer variable
    p := &x
    fmt.Println("address:", p)  // 0xc000...
    fmt.Println("via ptr:", *p) // 11
}

Go does NOT have pointer arithmetic (no p++). Pointers are safe — the garbage collector tracks them.

new() vs &

p1 := new(int)   // allocates, returns *int pointing to 0
*p1 = 42

x  := 42
p2 := &x         // pointer to existing variable

Both produce *int but new allocates fresh memory while & takes the address of a named variable.

0%