0

In the following code

var a int
var b interface{}

b = a

fmt.Printf("%T, %T \n", a, &a)
fmt.Printf("%T, %T \n", b, &b)

output:

int, *int 
int, *interface {}

I would expect the type of &b to be a pointer on int.

I have two questions:

1) Why is it a pointer on interface{} ?

2) How could I get a pointer on the original type ?

icza
  • 389,944
  • 63
  • 907
  • 827
Nank
  • 159
  • 1
  • 4
  • Just a nit-pick/friendly piece of advice: avoid using `interface{}` as much as possible. It's a powerful tool, and sometimes you just can't avoid it, but it's a common mistake made by people who are new to the language to use `interface{}` when they really shouldn't – Elias Van Ootegem Feb 08 '19 at 17:29

2 Answers2

6

&b => this is the address operator applied on the variable b, whose type is interface{}. So &b will be a pointer of type *interface{}, pointing to the variable b. If you take the address of a variable of type T, the result will always be of type *T.

You cannot obtain the address of the variable a from b, because the assignment:

b = a

Simply copies the value of a into b. It wraps the value of a in an interface value of type interface{}, and stores this interface value into b. This value is completely detached from a.

In general, all assignments copy the values being assigned. There are no reference types in Go. The closest you can get to what you want is if you store the address of a in b in the first place, e.g.:

b = &a

Then you can use type assertion to get out a's address from b like this:

fmt.Printf("%T, %T \n", a, &a)
fmt.Printf("%T, %T \n", b, b.(*int))

This outputs (try it on the Go Playground):

int, *int
*int, *int

(Note: when you simply print b, since it is of an interface type, the fmt package prints the (concrete) value wrapped in it.)

See related questions:

How to get a pointer to a variable that's masked as an interface?

Changing pointer type and value under interface with reflection

icza
  • 389,944
  • 63
  • 907
  • 827
1

Just to complete icza's answer. In case if you don't know the type of the value stored in the interface (and, hence, you can't explicitly use type assertion), you can use reflect package:

var a int
var b interface{}
b = &a
fmt.Println(reflect.TypeOf(b))
vonaka
  • 913
  • 1
  • 11
  • 23