Sobes.tech
Junior

What is the name of the structure in Go that copies data during transfer?

sobes.tech AI

Answer from AI

In Go, passing a struct always copies its data. There is no special name for a struct that behaves exactly like this; it is a fundamental property of structs.

package main

import "fmt"

type Point struct {
	X, Y int
}

func modifyPoint(p Point) {
	// Modify the copy p, not the original struct
	p.X = 10
	p.Y = 20
	fmt.Printf("Inside function: %v\n", p)
}

func main() {
	pt := Point{X: 1, Y: 2}
	fmt.Printf("Before function call: %v\n", pt)
	modifyPoint(pt)
	fmt.Printf("After function call: %v\n", pt) // pt remains unchanged
}