// example1.go
packge main
func main() {
s := make([]byte, 1024, 1024)
_ = s
}
s will be allocated in stack and lookup assemble code not call runtime.makeslice
// example2.go
packge main
func main() {
cap := 1024
s := make([]byte, 1024, cap)
_ = s
}
s will be allocated in heap and lookup assemble code there is runtime.makeslice why this ???
// example3.go
package main
func main() {
a := 100
if a>1 {
a = 1000
}
b := interface{}(a)
_ = b
}
lookup the assemble code and see the compiler is very clever to opt that but in example2.go why don't do this
I think it's the variable nature of your cap
variable - if you change this to const cap = 1024
the runtime.makeslice
call disappears. It seems the Go compiler* is currently unable to infer that cap
doesn't mutate, i.e. not change in value. As you state; in the last example a
is evaluated to 1000 at compile time.
* go version go1.7.1 linux/amd64