Pass By reference in golang [duplicate] - go

Say I want to pass a pointer to a function and change the value of a struct which that pointer points to by doing so. I would normally do this by dereferencing the pointer:
type Test struct { Value int}
func main() {
var i Test = Test {2}
var p *Test = &i
f(p)
println(i.Value) // 4
}
func f(p *Test) {
*p = Test{4}
}
My question is, why this code doesn't change the value
type Test struct { Value int}
func main() {
var i Test = Test {2}
var p *Test = &i
f(p)
println(i.Value) // 2
}
func f(p *Test) {
// ?
p = &Test{4}
}
while this one does:
type Test struct { Value int}
func main() {
var i Test = Test {2}
var p *Test = &i
f(p)
println(i.Value) // 4
}
func f(p *Test) {
p.Value = 4
}

Because this line:
p = &Test{4}
Just assigns a new pointer value to the p variable. Inside the f() function, p is just a local variable. By assigning any new value to p, you are just changing the value of the local variable and not the pointed value.
The p local variable in f() has nothing to do with the p local variable in main(). If you change p in f(), it will not change p in main() (and it won't change the pointed struct value either).
In your second example:
p.Value = 4
It is a shorthand for:
(*p).Value = 4
This changes the pointed value, hence you will observe the change when f() returns.
Note:
Just as a side note, if in your main() function you would pass the address of p (the local variable in main() which is a pointer) to function f(), you could modify the address stored in main's p:
func f(p **Test) {
*p = &Test{4}
}
And from main(), call it like:
var i Test = Test{2}
var p *Test = &i
f(&p)
println(i.Value) // 2 - Note that 'i' won't change!
println(p.Value) // 4 - Only the address in 'p' to the new struct value created in f()
But obviously passing a single pointer *Test and modifying the pointed value (p.Value = 4) is more efficient, much more convenient and much cleaner.

Related

How to make nil interface to struct in golang

I'm new in Golang.
I executed the code below. I get empty humans array in the end.
What should I do in func F?
For testing(monkeypatch) sake. I have to follow the way how the origin func is called.
package main
import (
"fmt"
)
type Human struct {
Name string
}
type Cat struct {
Name string
}
func F(arr interface{}) {
switch arr.(type) {
case *[]*Human:
arr = &[]*Human{{Name: "abc"}}
arr = arr.(*[]*Human)
case *[]*Cat:
arr = &[]*Cat{{Name: "meow"}}
arr = arr.(*[]*Cat)
}
}
func main() {
var humans []*Human
F(&humans)
fmt.Println(humans)
var cats []*Cat
F(&cats)
fmt.Println(cats)
}
The answer, and the main issue cause as well, is that Go always uses pass by value (or copy of the value) when arguments are passed around to function or assigned to variables.
Your function F takes an arr argument:
func F(arr interface{}) {
//...
}
When called from your main function, you are passing an []*Human pointer as an argument, which values will be copied and fed to your function F for execution.
Going back to your function F body, the arr will be having the same value passed by main, which happens to be the address to the original []*Human struct. Upon assigning a new value to arr:
func F(arr interface{}) {
switch arr.(type) {
case *[]*Human:
arr = &[]*Human{{Name: "abc"}}
// ...
}
}
You are assigning a new value to the local arr variable and not to the original pointer, which remains, indeed, unchanged.
To update the value toward which the argument pointer is referring to, you should used the dereferrence symbol:
func F(arr interface{}) {
switch arr := arr.(type) {
case *[]*Human:
*arr = []*Human{&Human{Name: "abc"}}
fmt.Println(arr)
// ...
}
}
Note the switch arr := arr.(type) statement which creates a new arr variable (shadowing the argument arr) with the interface dynamic type to be able to assign the proper value to it.

use unsafe pointer to get value from a []string

I am trying to learn how pointers work on go. Why is the following example not working?
package main
import (
"fmt"
"unsafe"
)
type SliceOfStrings []string
// function that creates an slice of []string
// returns interface{} cause I am interested on learning how pointers work
func Create() interface{} {
var mySlice1 SliceOfStrings = make([]string, 0)
mySlice1 = append(mySlice1, "str1")
mySlice1 = append(mySlice1, "str2")
// return a slice with as ["str1","str2"]
return mySlice1
}
func main() {
x := Create()
// 0xc000021940
fmt.Printf("address of x is %p \n", &x)
// get unsafe pointer to address of x
// unsafe pointer. Prints 0xc000021940
p1 := unsafe.Pointer(&x)
fmt.Println(p1)
// unsigned pointer. Prints 824633858368
p2 := uintptr(p1)
fmt.Println(p2)
// prints same value as p1 0xc000021940
p3 := unsafe.Pointer(p2)
fmt.Println(p3)
// Make p4 point to same address as 0xc000021940
p4 := (*SliceOfStrings)(p3)
//fmt.Println(p4)
// why this does not print "str1" ??
fmt.Println((*p4)[0])
// I get error: runtime error: invalid memory address or nil pointer dereference
}
Create() returns a value of type interface{}, so type of x is interface{}, so type of &x is *interface{}, and not *SliceOfStrings. So x points to an interface{} value and not to a SliceOfStrings value!
If you type assert SliceOfStrings from the return value of Create(), it works:
x := Create().(SliceOfStrings)
Also add runtime.KeepAlive(x) at the end of your main(), because if you don't refer to x anymore, it can be garbage collected at any time.
With this change it works and outputs str1. Try it on the Go Playground.
In general, stay away from package unsafe as much as possible. You can learn and use pointers without package unsafe. Only think of unsafe as a last-last resort!
I was able to understand why this happens:
package main
import (
"fmt"
"unsafe"
)
type SliceOfStrings []string
// when passing a slice to a method you are passing this data. Lets prove it
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
func main() {
// On go everything is passed by coping values. When you pass a slice to a function you are passing this:
// reference: https://stackoverflow.com/a/39993797/637142
/*
type SliceHeader struct {
Data uintptr
Len int
Cap int
}
*/
// create a new slice
var mySlice1 SliceOfStrings = make([]string, 0)
// when appending we need to keep the new content that is why we reasig it
mySlice1 = append(mySlice1, "str1")
mySlice1 = append(mySlice1, "str2")
// in other words this will make no sense:
// _ = append(mySlice1, "str3") // doing this will lose the new header value
// lets see the content of header mySlice
var pointerToMySlice1 = unsafe.Pointer(&mySlice1)
var header *SliceHeader = ((*SliceHeader)(pointerToMySlice1))
fmt.Println(*header)
// {824634220576 2 2}
// lets copy that header to another slice
var copy SliceOfStrings = mySlice1
var pointerToCopy = unsafe.Pointer(&copy)
header = ((*SliceHeader)(pointerToCopy))
fmt.Println(*header)
// prints the same thing
// {824634220576 2 2}
// now lets do the same thing but with an interface
var copy2 interface{} = mySlice1
var pointerToCopy2 = unsafe.Pointer(&copy2)
header = ((*SliceHeader)(pointerToCopy2))
fmt.Println(*header)
// this prints!
// {4845280 824634375976 0}
// I dont understand why this happens. But this is the reason why the example does not work
// I was trying to access an array from memory address 4845280 that is wrong
// now lets do what izca told us
var copy3 interface{} = mySlice1
tmp := (copy3).(SliceOfStrings)
var pointerToCopy3 = unsafe.Pointer(&tmp)
header = ((*SliceHeader)(pointerToCopy3))
fmt.Println(*header)
// this prints!
// {824634220576 2 2}
// that is the correct value
// lets try to get the array from that memory address (824634220576)
pointerToActualArray := unsafe.Pointer(header.Data)
// lets cast that to (*[2]string)
var pointerFinal *[2]string = ((*[2]string)(pointerToActualArray))
// now print the first value
fmt.Println((*pointerFinal)[0])
// prints str1
}

Don't understand variables

I'm confused with variables.
Case 1:
package main
var a = "F"
func main() {
n()
m()
n()
}
func n() {
print(a)
}
func m() {
a = "O"
print(a)
}
result = FOO
But when I change a = "O" on (var a = "O" or a := "O") result is FOF.
var a = "O" and a := "O" both declares new variables. That means the a inside m is a different a then the global variable, so setting it to O doesn't change the value of the global one.
You are allowed to shadow variables like this if they are in different scopes, which they are in this case.
One is in the global scope, and one is in the scope of m function.

Passing slices to a function

I have some confusion regarding passign slices to function. Here is what I have readed:
Here are what I have understood: slice is a structure with a pointer to real data; when we are passing a slice to a function, we just copy a pointer, but the function is working with the same data as original function.
Here is my code:
type Example struct {
A int
B string
}
func foo(d []Example) {
for _, e := range d {
e.B = "bye"
}
}
func main() {
a := Example{}
a.A = 10
a.B = "hello"
b := Example{}
b.A = 10
b.B = "hello"
var c []Example
c = append(c, a)
c = append(c, b)
foo(c)
for _, e := range c {
fmt.Println(e.B)
}
}
I have passed slice of structs to a function and have changed the struct in the function. Why I have old values in the main function ?
Because it's a slice of structs, not a slice of pointers to structs. When you execute:
for _, e := range d
Inside the loop, e is a copy of the element from the slice; modifying it does not modify what's in the slice. If d were a []*Example, it would work as you expected: https://play.golang.org/p/4ZgLETpq6d0
Note in particular that this has nothing at all to do with slices. If it were:
func foo(d Example) {
d.B = "bye"
}
You would run into the same problem: the function is modifying a copy of the struct, so the caller's copy is unaffected by what happens inside the function.
Another potential solution without using pointers would be to modify the values inside the slice, rather than in a copy of the element:
func foo(d []Example) {
for i := range d {
d[i].B = "bye"
}
}
Working example of this style: https://play.golang.org/p/_UJGU0XqaUO

What's the difference between these functions in golang?

I'm new to Go programming and wondering what's the difference (if any) there between
a.
func DoSomething(a *A) {
b = a
}
b.
func DoSomething(a A) {
b = &a
}
If you are actually asking what the difference of those b's are, one is a pointer to the object passed as an argument to DoSomething, and the other is a pointer to a copy of the object passed as an argument to DoSomething.
https://play.golang.org/p/ush0hDZsdE
type A struct {
f string
}
func DoSomethingPtr(a *A) {
b := a
b.f = "hi"
}
func DoSomething(a A) {
b := &a
b.f = "hey"
}
func main() {
x := A{"hello"}
DoSomething(x)
fmt.Println(x)
DoSomethingPtr(&x)
fmt.Println(x)
}
The variable b would be assigned a different value in each function. The values are different because one is passing a copied value and the other is passing a pointer to the original value in memory.
package main
import "fmt"
type A string
func DoSomethingPtr(a *A) {
fmt.Println(a)
}
func DoSomething(a A) {
fmt.Println(&a)
}
func main() {
x := A("hello")
DoSomething(x)
DoSomethingPtr(&x)
}
Here is the executable proof.
In general, these two functions will assign different values to b. The second one makes a copy of the argument, and so the a inside the function generally has a different memory address than whatever input is passed into the function. See this playground example
package main
type A struct{
x int
}
var b *A
func d(a *A) {
b = a
}
func e(a A) {
b = &a
}
func main() {
var a = A{3}
println(&a)
d(&a)
println(b)
e(a)
println(b)
}
Interestingly, if you make the type A an empty struct instead, and initialize var a = A{}, you actually see the same value for b in the println statements.
That's because for the empty-struct type, there can only really only ever be 1 value, and its immutable, so all instances of it share the same memory address?

Resources