What does the asterisk do in "Go"?
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 through and trying to understand the examples on the Go website and I keep coming across a special asterisk character in examples like this:
s := "hello"
if s[1] != 'e' {
os.Exit(1)
}
s = "good bye"
var p *string = &s
*p = "ciao"
Also, I just noticed, what's with the &s
? Is it assignment by reference (I might be using PHP talk here)?
Solution 1:
*
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 or to the field.
Solution 2:
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!
Solution 3:
Go lang Addresses, Pointers and Types:
s := "hello" // type string
t := "bye" // type string
u := 44 // type int
v := [2]int{1, 2} // type array
q := &s // type pointer
All these Go variables have an address in memory. The distinction between them is string types hold string values, int types hold integer values, and pointer types hold addresses. So even variables that hold addresses have their own memory address.
Pointers:
Adding &
before a variable name evaluates to it's address. Or think, "here's my address so you know where to find me."
// make p type pointer (to string only) and assign value to address of s
var p *string = &s // type *string
// or
q := &s // shorthand, same deal
Adding *
before a pointer variable dereferences the pointer to the address it is holding. Or think, "pass the action on to the address which is my value."
*p = "ciao" // change s, not p, the value of p remains the address of s
// j := *s // error, s is not a pointer type, no address to redirect action to
// p = "ciao" // error, can't change to type string
p = &t // change p, now points to address of t
//p = &u // error, can't change to type *int
// make r type pointer (to a pointer which is a pointer to a string) and assign value to address of p
var r **string = &p // shorthand: r := &p
w := ( r == &p) // ( r evaluates to address of p) w = true
w = ( *r == p ) // ( *r evaluates to value of p [address of t]) w = true
w = (**r == t ) // (**r evaluates to value of t) w = true
// make n type pointer (to string) and assign value to address of t (deref'd p)
n := &*p
o := *&t // meaningless flip-flop, same as: o := t
// point y to array v
y := &v
z := (*y)[0] // dereference y, get first value of element, assign to z (z == 1)
Go Play here: http://play.golang.org/p/u3sPpYLfz7