0Pricing
Go Academy · Lesson

Pointers and Functions

Passing by value vs passing by pointer

Pointers and Functions is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Pass by Value

Go passes all function arguments by value — a copy is made. Changes inside the function don't affect the original:

package main
import "fmt"

func increment(n int) { n++ }

func main() {
    x := 5
    increment(x)
    fmt.Println(x) // 5 — unchanged
}

Pass by Pointer

Pass a pointer to let the function mutate the original:

package main
import "fmt"

func increment(n *int) { *n++ }

func main() {
    x := 5
    increment(&x)
    fmt.Println(x) // 6
}

Returning Pointers

Functions can return pointers to heap-allocated values. Go's escape analysis ensures safety:

package main
import "fmt"

func newInt(v int) *int { return &v }

func main() {
    p := newInt(42)
    fmt.Println(*p) // 42
}

Large Struct Efficiency

Passing a large struct by pointer avoids an expensive copy:

package main
import "fmt"

type BigData struct{ Data [1024]int }

func process(d *BigData) { d.Data[0] = 99 }

func main() {
    bd := BigData{}
    process(&bd)
    fmt.Println(bd.Data[0]) // 99
}

Pointer to Interface

Rarely needed — interfaces already hold a pointer internally. Passing *interface{} is almost always wrong:

package main
import "fmt"

type Stringer interface{ String() string }

func print(s Stringer) { fmt.Println(s.String()) }

// Pass interface directly, NOT *Stringer
type Name string
func (n Name) String() string { return string(n) }

func main() {
    print(Name("Alice"))
}

Optional Parameters with Pointers

Use pointer parameters to distinguish "not provided" from zero value:

package main
import "fmt"

func greet(name *string) {
    if name == nil {
        fmt.Println("Hello, stranger")
        return
    }
    fmt.Println("Hello,", *name)
}

func main() {
    s := "Alice"
    greet(&s)
    greet(nil)
}

Mutation Through Slices

Slices already contain an internal pointer, so you often don't need *[]T. However, when append reallocates, you do need a pointer to the slice itself:

package main
import "fmt"

func addItem(s *[]int, v int) { *s = append(*s, v) }

func main() {
    nums := []int{1, 2, 3}
    addItem(&nums, 4)
    fmt.Println(nums) // [1 2 3 4]
}

Pointer Aliasing

Multiple pointers to the same variable create aliasing — a change through one pointer is visible through all others:

package main
import "fmt"

func main() {
    x := 10
    a, b := &x, &x
    *a = 99
    fmt.Println(*b) // 99 — same memory
}

Avoiding Pointer Overuse

Use pointers only when necessary:

  • Mutation inside a function
  • Large structs (>= ~64 bytes)
  • Nil to mean "absent"

Overusing pointers adds GC pressure and reduces code clarity.

Function Pointer Pattern

Store function pointers in a map for dispatch tables:

package main
import "fmt"

func add(a, b int) int { return a + b }
func sub(a, b int) int { return a - b }

func main() {
    ops := map[string]func(int,int)int{
        "+": add, "-": sub,
    }
    fmt.Println(ops["+"](10, 3)) // 13
    fmt.Println(ops["-"](10, 3)) // 7
}

Quick Check

How do you let a function modify an integer variable defined in the caller?

Recap

Key takeaways:

  • Go is pass-by-value — use pointers for mutation
  • Return pointers from constructors for heap allocation
  • Slices carry an internal pointer but append may require *[]T
  • Avoid overusing pointers — prefer values for small types

Practice Prompt

Write a normalize function that takes *[]string and converts all strings to lowercase in-place using strings.ToLower.

Frequently asked questions

Is the “Pointers and Functions” lesson free?

Yes — the full text of “Pointers and Functions” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Pointers and Functions”?

Passing by value vs passing by pointer You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Pointers and Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Are Pointers?
  2. Pointers and Functions
  3. Pointer Receivers on Methods
  4. new() and When to Use Pointers
← Back to Go Academy