for true {...} and for {...} are not equivalent
make built-in function to create a map, the second argument (cap) can be negative
aConstantString[:] is not a constant
package main
var a, b [0]int
var p, q = &a, &b
func main() {
if (p == q) {
p, q = &a, &b
println(p == q)
}
}
package main
var a, b struct{}
var p, q = &a, &b
func main() {
var p2, q2 = &a, &b
println(p == q) // true
println(p == p2) // true
println(q == q2) // true
println(p == q2) // true
println(q == p2) // true
println(p2 == q2) // false
}
for true {...} and for {...} are not equivalent
foo shown below compiles, but bar doesn't.
func foo() int {
for {}
}
func bar() int {
for true {}
}
make built-in function to create a map, the second argument (cap) can be negative
package main
var n = -100
func main() {
var m = make(map[int]bool, n)
m[123] = true;
}
cap argument is equivalent to omitting it.
aConstantString[:] is not a constant
package main
const s = "zigo 101" // len(s) == 8
var a byte = 1 << len(s) / 128
var b byte = 1 << len(s[:]) / 128
func main() {
println(a, b) // 2 0
}
0 0 2.
package main
const N = 8
var n = N
func main() {
var x byte = 1 << n / 128
var y = byte(1 << n / 128)
var z byte = 1 << N / 128
println(x, y, z) // 0 0 2
}
1 are both assumed as byte. So the two declarations are both equivalent to the following line, in which the run-time expression byte(1) << n overflows (and is truncated to 0).
... = byte(1) << n // 128
1 << N / 128 is evaluated at compile time. In the expression, 1 is treated as untyped int value.
var z = 2
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.