tags:

views:

186

answers:

4

I'm a web developer looking to expand my horizons in order to get better at programming as a whole. I've done a bit Java and some simple Android applications. I'm now looking into lower level languages like C and Go (which I must say has some beautiful syntax and great ideas thus far, though I'm maybe too inexperienced to comment).

So yeah I've been going though and trying to understand the examples on the Go website and I keep coming across a special asterisk character in example like this:

s := "hello"
if s[1] != 'e' {
    os.Exit(1)
}
s = "good bye"
var p *string = &s
*p = "ciao"

Also, I just noticed, whats with the "&s" is it assignment by reference (I might be using PHP talk here)?

Thanks.

+3  A: 

I don't know Go, but based on the syntax, it seems that its similar to C - That is a pointer. Its similar to a reference, but lower level and more powerful. It contains the memory address of the item in question. &a gets the memory address of a variable and *a dereferences it, getting the value at the memory address.

Also, the * in the declaration means that it is a pointer.

So yes, its like in PHP in that the value of s is changed because p and &s point to the same block of memory.

mathepic
Matchu
Matchu
I know it's not accurate, the PHP website itself says that references are not pointers but references are the closest thing I have to actually truly understanding what pointers are.
rich97
It allows you to do the same thing that PHP references allow you to do in a different way. I was in no way saying that PHP references = Go pointers.
mathepic
+3  A: 

The * character is used to define a pointer in both C and Go. Instead of a real value the variable instead has an address to the location of a value. The & operator is used to take the address of an object.

JaredPar
Just a note - A memory address is itself a real value. Thats why (in C, but not Go according to the FAQ) you can perform arithmetic on pointers.
mathepic
+5  A: 

Im guessing it means the same as in C

p is a pointer to a string

The statement var p *string = &s would assign the address of the s object to p

Next line *p = "ciao" would change the contents of s

See this link from the Language Design FAQ

Interestingly, no pointer arithmetic

Why is there no pointer arithmetic? Safety. Without pointer arithmetic it's possible to create a language that can never derive an illegal address that succeeds incorrectly. Compiler and hardware technology have advanced to the point where a loop using array indices can be as efficient as a loop using pointer arithmetic. Also, the lack of pointer arithmetic can simplify the implementation of the garbage collector.

Now I want to start learning GO!

Tom
Learn Go! I highly recommend it.
jcao219
rich97
Evan Shaw
It does indeed. Thanks!
rich97
+2  A: 

* attached to a type (*string) indicates a pointer to the type.

* attached to a variable in an assignment (*v = ...) indicates an indirect assignment. That is, change the value pointed at by the variable.

* attached to a variable or expression (*v) indicates a pointer dereference. That is, take the value the variable is pointing at.

& attached to a variable or expression (&v) indicates a reference. That is, create a pointer to the value of the variable.

MizardX