golang中的 string(line)源码实现在哪里

问题:这行代码

str := string(line)

line是 []byte 类型, 我想查看string函数的实现, 在哪里可以看到?

点击string函数 跳转到

// 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函数

阅读 3.4k
1 个回答

string() 函数的具体实现与调用它的参数类型有关,与 go 版本也有关。


举个例子,以下代码

package main

import "fmt"

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

你可以运行 go build -gcflags -S xxx.go 查看汇编信息

...
CALL    runtime.slicebytetostring(SB)
...
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题