为什么常量会转义以堆积在golang中?

Here is my golang code to set a key C to value 1 in redis

It uses third party code in order to connect to redis.

package main

import (
    "github.com/garyburd/redigo/redis"
)

func main() {

    c, err := redis.Dial("tcp", ":6379")

    _, err = c.Do("SET", "C", 1)
    if err != nil {
        panic(err)
    }
}

On doing go build -gcflags "-m -m", it reports

./del.go:41: 1 escapes to heap

./del.go:41: from c.Do("SET", "C", 1) (parameter to indirect call) at ./del.go:41

Why does 1 escape to the heap ? It is a simple constant whose complete information is known at compile time and it takes just 64bits on a 64bit system. Why should it be stored in heap memory?

The signature of Do is:

Do(commandName string, args ...interface{}) (reply interface{}, err error)

Because args is a variadic (slice of) interface{} it's heap allocated.

I believe there could be some optimisations in the pipeline for simple cases like this in future versions of Go: https://github.com/golang/go/issues/15528