鍍金池/ 問答/GO  Linux/ golang中的 string(line)源碼實現(xiàn)在哪里

golang中的 string(line)源碼實現(xiàn)在哪里

問題:這行代碼

str := string(line)

line是 []byte 類型, 我想查看string函數(shù)的實現(xiàn), 在哪里可以看到?

點擊string函數(shù) 跳轉(zhuǎn)到

// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string

找不到string函數(shù)

回答
編輯回答
苦妄

string() 函數(shù)的具體實現(xiàn)與調(diào)用它的參數(shù)類型有關(guān),與 go 版本也有關(guān)。


舉個例子,以下代碼

package main

import "fmt"

func main() {
    b := make([]byte, 10)
    fmt.Println(string(b))
}

你可以運(yùn)行 go build -gcflags -S xxx.go 查看匯編信息

...
CALL    runtime.slicebytetostring(SB)
...
2018年1月11日 01:43