请帮助我理解这一点,也许我做错了什么。
func main() {
x := 6
y := &x
fmt.Println("x: ", x, ", y:", *y, "string y:", string(*y))
}
返回:
x: 6 , y: 6 string y:
为什么 string(*y) 不返回 6 ?
最佳答案
Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer.
表达式 string(*y)
的计算结果为 "\x06"
。这是一个字节为6的字符串。
使用strconv.Itoa将整数转换为字符串:strconv.Itoa(*y) == "6"
。
请注意,*y
的计算结果为 6。应用程序中使用的指针取消引用对字符串类型转换的执行方式没有影响。
关于string - golang int 对字符串的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47600029/