Sobes.tech
Junior — Senior

Analysis of passing a pointer to a structure to a function

livecode

Task condition

The task requires examining how data structures change when their pointer is passed to a function, and what values will be printed before and after the change inside the function.

package main

import "fmt"

type Person struct {
    Name string
}

func changeName(p1 *Person) {
    *p1 = Person{Name: "Alice"}
}

func main() {
    p := &Person{Name: "Bob"}

    fmt.Println(p.Name)

    changeName(p)

    fmt.Println(p.Name)
}