Go 普通字符串打印问题,println 和 string() 包裹一下有什么区别?

为什么一个简单的字符串我使用内置函数 Println 可以打印出来,但使用 string 包了一下就打不出来了

image.png

阅读 2.3k
2 个回答

看前面的 if ,openCapcha 应该不是一个字符串,而是一个整形。整形转字符串直接得到的是这个这个码位的 Unicode 字符:

Conversions

Conversions to and from a string type

Converting a signed or unsigned integer value to a string type yields a string containing the UTF-8 representation of the integer. Values outside the range of valid Unicode code points are converted to "\uFFFD".

    string('a')       // "a"
    string(-1)        // "\ufffd" == "\xef\xbf\xbd"
    string(0xf8)      // "\u00f8" == "ø" == "\xc3\xb8"

    type myString string
    myString(0x65e5)  // "\u65e5" == "日" == "\xe6\x97\xa5"

这个转换应该不是你需要的。

你需要的应该是 strconv.Itoa

你那个openCaptcha是个整型3,string(3)转成了ASCII编码3对应的字符了,是你那ide的控制台显示不出来,上go.dev/play打印一下你就能看见了。

推荐问题