本节重点:
- 学会 sting 的基本使用
字符串在 Go 中值得特别提及,因为与其他语言相比,string 的实现方式同其他语言略有不同。
什么是字符串?字符串是 Go 中的字节切片。可以通过将一组字符括在双引号中来创建字符串" "。
让我们看个创建string并打印它的简单示例。
package main
import (
"fmt"
)
func main() {
web := "https:www.gotribe.cn"
fmt.Println(web)
}
上面的程序将打印https:www.gotribe.cn.
Go 中的字符串是兼容Unicode编码的,并且是UTF-8编码的。
由于字符串是字节切片,因此可以访问字符串的每个字节。
package main
import (
"fmt"
)
func printBytes(s string) {
fmt.Printf("Bytes: ")
for i := 0; i < len(s); i++ {
fmt.Printf("%x ", s[i])
}
}
func main() {
web := "https:www.gotribe.cn"
fmt.Printf("String: %sn", web)
printB


