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
Use Field Tags in the Definition of Struct Type
Program to print pyramid using numbers
Golang program for implementation of Levenshtein distance
How do you set headers in an HTTP request with an HTTP client in Go?
Launches 10 Goroutines and each goroutine adding 10 values to a Channel
Go program to find PTR pointer record of a domain
How to convert Struct fields into Map String?
Declaration of a struct type
Illustration of Producer Consumer Problem in Golang
How to read current directory using Readdir?