Example of Pointers with Struct
The example struct have 2 variables X and Y of string type. Other variables used to assign the values of struct pointers.
Example
package main
import "fmt"
type example struct {
X, Y string
}
var (
a = example{"Aus","Can"}
b = &example{"Jap","Kor"}
c = example{X:"US",Y:"UK"}
d = example{}
)
func main() {
e := b
b.X = "Rus"
f := *b
fmt.Println("a:\t",a)
fmt.Println("b:\t",b)
fmt.Println("c:\t",c)
fmt.Println("d:\t",d)
fmt.Println("e:\t",e)
fmt.Println("e:\t",f)
}
Output
a: {Aus Can}
b: &{Rus Kor}
c: {US UK}
d: { }
e: &{Rus Kor}
e: {Rus Kor}
Most Helpful This Week
Simple function with return value in Golang
Different ways to validate JSON string
How to declare and access pointer variable?
How to play and pause execution of goroutine?
How pointer & and * and ** works in Golang?
Example: How to use ReadFull from IO Package in Golang?
How to import and alias package names?
Golang import package inside package
How to declare empty Map in Go?
How to check string contains uppercase lowercase character in Golang?