Struct semantics

Struct value copying

Assignment copies struct field values. A slice, map or pointer field may continue to reference shared data; copying a struct is not a general deep clone. Avoid copying types whose contracts forbid copying after use. Example output: '1 new 2'.

Formal Syntax & Structure

go syntaxgo
b := a

Working Example

Example Snippetgo
package main
import "fmt"
type Record struct {  N    int  Tags []string}
func main() {  a := Record{N: 1, Tags: []string{"old"}}  b := a  b.N = 2  b.Tags[0] = "new"  fmt.Println(a.N, a.Tags[0], b.N)}
Go Reference