以append这个builtin函数为例,我们可以在Go安装包的src/builtin/builtin.go中找到它的原型:

// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
// slice = append(slice, elem1, elem2)
// slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
// slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type

但这里没有append的实现。append在编译时会被编译器替换为若干runtime的调用。我们可以输出下面代码的汇编:

package main
 
import "fmt"

func main() {
 var s = []int{5, 6}
 s = append(s, 7, 8)
 fmt.Println(s)
}

$go tool compile -S xxx.go > xxx.s

汇编节选如下(xxx.s):

"".main STEXT size=277 args=0x0 locals=0x58
 0x0000 00000 (xxx.go:5) TEXT "".main(SB), ABIInternal, $88-0
 0x0000 00000 (xxx.go:5) MOVQ (TLS), CX
 0x0009 00009 (xxx.go:5) CMPQ SP, 16(CX)
 0x000d 00013 (xxx.go:5) PCDATA $0, $-2
 0x000d 00013 (xxx.go:5) JLS  267
 0x0013 00019 (xxx.go:5) PCDATA $0, $-1
 0x0013 00019 (xxx.go:5) SUBQ $88, SP
 0x0017 00023 (xxx.go:5) MOVQ BP, 80(SP)
 0x001c 00028 (xxx.go:5) LEAQ 80(SP), BP
 0x0021 00033 (xxx.go:5) PCDATA $0, $-2
 0x0021 00033 (xxx.go:5) PCDATA $1, $-2
 0x0021 00033 (xxx.go:5) FUNCDATA $0, gclocals·69c1753bd5f81501d95132d08af04464(SB)
 0x0021 00033 (xxx.go:5) FUNCDATA $1, gclocals·568470801006e5c0dc3947ea998fe279(SB)
 0x0021 00033 (xxx.go:5) FUNCDATA $2, gclocals·bfec7e55b3f043d1941c093912808913(SB)
 0x0021 00033 (xxx.go:5) FUNCDATA $3, "".main.stkobj(SB)
 0x0021 00033 (xxx.go:6) PCDATA $0, $1
 0x0021 00033 (xxx.go:6) PCDATA $1, $0
 0x0021 00033 (xxx.go:6) LEAQ type.[2]int(SB), AX
 0x0028 00040 (xxx.go:6) PCDATA $0, $0
 0x0028 00040 (xxx.go:6) MOVQ AX, (SP)
 0x002c 00044 (xxx.go:6) CALL runtime.newobject(SB)
 0x0031 00049 (xxx.go:6) PCDATA $0, $1
 0x0031 00049 (xxx.go:6) MOVQ 8(SP), AX
 0x0036 00054 (xxx.go:6) MOVQ $5, (AX)
 0x003d 00061 (xxx.go:6) MOVQ $6, 8(AX)
 0x0045 00069 (xxx.go:7) PCDATA $0, $2
 0x0045 00069 (xxx.go:7) LEAQ type.int(SB), CX
 0x004c 00076 (xxx.go:7) PCDATA $0, $1
 0x004c 00076 (xxx.go:7) MOVQ CX, (SP)
 0x0050 00080 (xxx.go:7) PCDATA $0, $0
 0x0050 00080 (xxx.go:7) MOVQ AX, 8(SP)
 0x0055 00085 (xxx.go:7) MOVQ $2, 16(SP)
 0x005e 00094 (xxx.go:7) MOVQ $2, 24(SP)
 0x0067 00103 (xxx.go:7) MOVQ $4, 32(SP)
 0x0070 00112 (xxx.go:7) CALL runtime.growslice(SB)
 0x0075 00117 (xxx.go:7) PCDATA $0, $1
 0x0075 00117 (xxx.go:7) MOVQ 40(SP), AX
 0x007a 00122 (xxx.go:7) MOVQ 48(SP), CX
 0x007f 00127 (xxx.go:7) MOVQ 56(SP), DX
 0x0084 00132 (xxx.go:7) MOVQ $7, 16(AX)
 0x008c 00140 (xxx.go:7) MOVQ $8, 24(AX)
 0x0094 00148 (xxx.go:8) PCDATA $0, $0
 0x0094 00148 (xxx.go:8) MOVQ AX, (SP)
 0x0098 00152 (xxx.go:7) LEAQ 2(CX), AX
 0x009c 00156 (xxx.go:8) MOVQ AX, 8(SP)
 0x00a1 00161 (xxx.go:8) MOVQ DX, 16(SP)
 0x00a6 00166 (xxx.go:8) CALL runtime.convTslice(SB)
 ... ...


你可以看到append被换成:runtime.growslice以及相关汇编指令了。