Copy Struct Type Using Value and Pointer Reference
r2 will be the same as r1, it is a copy of r1 rather than a reference to it. Any changes made to r2 will not be applied to r1 and vice versa. When r3 is updated, the underlying memory that is assigned to r1 is updated.
Example
package main
import "fmt"
type rectangle struct {
length float64
breadth float64
color string
}
func main() {
r1 := rectangle{10, 20, "Green"}
fmt.Println(r1)
r2 := r1
r2.color = "Pink"
fmt.Println(r2)
r3 := &r1
r3.color = "Red"
fmt.Println(r3)
fmt.Println(r1)
}
Output
{10 20 Green}
{10 20 Pink}
&{10 20 Red}
{10 20 Red}
Most Helpful This Week
Illustration of Checkpoint Synchronization in Golang
How append a slice to an existing slice in Golang?
Panic: runtime error: index out of range error in Golang
Golang program to generate number of slices permutations of number entered by user
How to build a map of struct and append values to it?
How do you create an HTTP server in Go?