patterngoMajor
What does the asterisk do in "Go"?
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:
Also, I just noticed, what's with the
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
The statement
Next line
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.
p is a pointer to a stringThe statement
var p *string = &s would assign the address of the s object to pNext line
*p = "ciao" would change the contents of sSee 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.