字符串赋值后就不能修改
go
var str string = "abcd"
str[0] = 'f' //这里就有能修改str内容
字符串的两种表示形式
- 双引号,会识别转义字符
- 反引号,以字符串的原生形式输出,包括换行和特殊字符,可以实现防止攻击、输出输出源代码等效果
go
var str2 = "abc\ncba"
fmt.Println(str2)
var str3 string = `
package main
import "fmt"
import "time"
import "unsafe"
func main(){
//go语言类型转换 type_name(expression)
var sum int = 17
var count int = 20
var mean float32
mean = float32(sum)/float32(count)
fmt.Println(mean)
}
`
fmt.Println(str3)
字符串拼接方式
go
str4 := "hello " + "world!"
str4 += " haha!"
fmt.Println(str4)
当一行字符串太长时,可以将+号保留在上一行
go
str4 += "hello " + "world " + "hello " + "world " +
"hello " + "world " + "hello " + "world " +
"hello"
fmt.Println(str4)