(声明:此文章是本人学习尚硅谷的go语言核心编程课程所做的笔记,仅供学习使用,想看原版教学视频的请到官网,如有不妥联系本人删除)
1、字符串就是一串固定长度的字符连接起来的字符序列。但go的字符串是由单个字节连接起来的,
go的字符串的字节使用UTF-8编码标识unicode文本。
例子:
package main
import "fmt"
func main(){
var b string = "萨科罚金萨克洛夫"
fmt.Println(b) //输出:萨科罚金萨克洛夫
}
2、注意事项。
1)go语言的字符串使用UTF-8编码表示Unicode文本
2)字符串一但赋值了,就不能再修改,在go中是不可变的。
例子:
var str string = "hello world"
str[0] = 'a' //报错,不能修改
3)字符串的两种表达形式
双引号:会识别转义字符
反引号:以字符串的原生形式输出,包括换行和特殊字符,可以实现防止攻击,
输出源代码等效果,`sadsad`。
4)字符串拼接用 ‘+’ 号来完成
例子:
var str = "hello"+"world"
str+="hhhh"
fmt.Println(str)
当拼接操作很长是,分行写,需要将加号保留在上一行
str1 := "hello"+"hello"+"hello"+"hello"+"hello"+
"hello"+"hello"+"hello"+"hello";