package main
import "unsafe"
const S = "go" // S[1]-S[0] == 8
func main() {
var x *[8][8]byte
println(unsafe.Sizeof((*x)[S[1]-S[0]][S[1]-S[0]]))
}
Choices:
Answer: it princs 1.
Run it on Go play.
Key points:
S
, the expression S[i]
is always treated as a non-constant
(please read the stirngs in Go article or
Go Details and Tips book).
So the program compiles.
unsafe.Sizeof
calls are evaluated at compile time.
In the evaluations, only the type information of the arguments are used.
The expressions *x
and S[1]-S[0]
etc are never evaluated at run time.
So no "index out of range" panics will occur.
The Go 101 project is hosted on Github. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.
If you would like to learn some Go details and facts every serveral days, please follow Go 101's official Twitter account @zigo_101.