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
Golang program to generate number of slices permutations of number entered by user
Example: How to use ReadFull from IO Package in Golang?
How do you handle HTTP client server alerting in Go?
What is the default HTTP server in Go?
Golang program for implementation of Quick Sort
Golang program to demonstrates how to encode map data into a JSON string.
How to Iterate Over a Slice in Golang?
Program to print full pyramid using star
GO Program to take user input and addition of two strings
Golang program for implementation of Interpolation Search