package main
const S = "Go"
const G = S[0]
func main() {}
185
(-71 + 256).
package main
const S = "Go" // S[0] == 71
func main() {
println(-S[0])
}
-S[0]
is byte
(aka. uint8
), whereas -71
owerflows the range of byte
.
S
is a type parameter and [4]*int
is its constraint.
type T [S [4]*int] struct{}
S [4]
may be treated as constant byte
value if S
is a constant string. In the parsing phase, the compiler doesn't know what the identifier int
denotes. A type or an integer constant? If it denotes an integer constant, and S [4]
may be treated as constant byte
value, then the above line is a valid ordinary array type declaration.
S [4]
will never be constant, so it always tries to think the above line is not an ordinary array type declaration. No ambiguities happen here. Were elements of constant strings constants, the simplified type parameter declaration syntax couldn't be possible.
const A = [2]int{1, 2}
type BoolArray [A [1] * int]bool
const _ = "Google"[:2]
var _ = "Google"[:2]
128 0
(the reason is explained here).
package main
const S = "Go"
var a byte = 64 << len(S) / 2
var b byte = 64 << len(S[:]) / 2
func main() {
println(a, b) // 128 0
}
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.