Project: Summer Term 2026

Martin Sulzmann

How the project works

Each student will be assigned to one project (via a lottery).

Each project consists of a simple Go program (about 20 lines of code, similar complexity to the programs in the lecture notes).

Your task is to explain and modify the program and answer some questions.

Each student has to give an in-person presentation (about 5-10 minutes) and answer some additional questions.

The in-person presentation takes place in class (Tuesday 11:30-13:00 in E304) in week W14 and week W15.

P1: Struct vs Pointer

package main

import "fmt"

type pair struct {
    x int
    y int
}

func inc(p pair) {
    p.x++
}

func main() {
    p := pair{1, 2}
    inc(p)
    fmt.Println(p.x)
}

Tasks

  1. What is printed? Why?
  2. Modify the program so that p.x (in main) is incremented.
  3. Explain the difference between your version and the original.

P2: Slice Sharing

package main

import "fmt"

func modify(xs []int) {
    xs[0] = 42
}

func main() {
    a := []int{1, 2, 3}
    modify(a)
    fmt.Println(a[0])
}

Tasks

  1. What is printed? Why?
  2. Modify the program so that a is not changed.
  3. Explain what slices store internally.

P3: Array Copy

package main

import "fmt"

func modify(a [3]int) {
    a[0] = 99
}

func main() {
    x := [3]int{1, 2, 3}
    modify(x)
    fmt.Println(x[0])
}

Tasks

  1. What is printed? Why?
  2. Modify the program so that the change is visible in main.
  3. Compare arrays and slices.

P4: Range Copy Pitfall

package main

import "fmt"

func main() {
    xs := []int{1, 2, 3}

    for _, v := range xs {
        v++
    }

    fmt.Println(xs)
}

Tasks

  1. What is printed? Why?
  2. Fix the program so all elements are incremented.
  3. Explain how range works.

P5: Closure Capture

package main

import "fmt"

func main() {
    x := 1

    f := func() {
        x++
    }

    f()
    f()

    fmt.Println(x)
}

Tasks

  1. What is printed? Why?
  2. Modify the program so that f does not change x.
  3. Explain how closures capture variables.

P6: Slice + Append

package main

import "fmt"

func main() {
    xs := []int{1, 2}
    ys := xs

    xs = append(xs, 3)
    xs[0] = 99

    fmt.Println(ys[0])
}

Tasks

  1. What are possible outputs?
  2. Explain why the result is not always the same.
  3. Modify the program so the result is always predictable.

P7: Function Returning Function

package main

import "fmt"

func makeAdder(x int) func(int) int {
    return func(y int) int {
        return x + y
    }
}

func main() {
    add2 := makeAdder(2)
    fmt.Println(add2(3))
}

Tasks

  1. What is printed? Why?
  2. Explain the type of makeAdder.
  3. Modify the program so it multiplies instead of adds.

P8: Mixed Struct + Slice

package main

import "fmt"

type box struct {
    value int
}

func update(b box, xs []int) {
    b.value = 10
    xs[0] = 10
}

func main() {
    b := box{1}
    xs := []int{1, 2}

    update(b, xs)

    fmt.Println(b.value, xs[0])
}

Tasks

  1. What is printed? Why?
  2. Modify the program so both values change.
  3. Explain the difference in behavior.