HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoMajor

What does the asterisk do in "Go"?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
doeswhatasteriskthe

Problem

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

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.

Context

Stack Overflow Q#3552626, score: 84

Revisions (0)

No revisions yet.