K Prayogo
1 min readAug 8, 2021

--

for 2nd one is easier to grep/ripgrep if it's on one line, also we could write other method on other source code (for example: bla.codegen.go and bla.go, both have methods to modify bla struct)

for 1st one is simple really, it's less complicated than C/C++ pointer (no pointer arithmatic)

pointer = one that reference, it means you explicitly stating that you will modify the content of struct or you do not need a copy

```

i := 1

j := &i // j points to i

*j = 2 // change whatever variable j points to as 2

x := 3

j = &x

*j = 4 // change whatever variable j points to as 4

func kval(int x) { x = 2 }

kval(1) // valid

func kref(int *x) { *x = 3 }

kref(1) // not valid, since you are explicitly saying taht you will change the variable value

kref(&i) // valid

func (x *SomeStruct) change() { x.SomeProp = 1 }

y := SomeProp{}

y.change() // y.SomeProp changed to 1

func (x SomeStruct) changeCopy() SomeStruct {

x.SomeProp = 2

return x

}

z := y.changeCopy()

// z.SomeProp == 2

// y.SomeProp still 1

```

--

--

K Prayogo
K Prayogo

No responses yet