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
Find out how many logical processors used by current process
Golang writing struct to JSON file
What is risk management and why is it important?
Convert Int data type to Int16 Int32 Int64
How do you set cookies in an HTTP request with an HTTP client in Go?
Concurrently printing array elements using goroutines and channels