字符串类型
demo01
package main //hello.go所在的包的是main
import "fmt" //引入一个包 'fmt'
func main() {
//string的基本使用
var address string = "北京市昌平区天通苑"
fmt.Println("address=",address)
}
demo02
package main //hello.go所在的包的是main
import "fmt" //引入一个包 'fmt'
func main() {
//string的值不能单独修改,只能重新赋值
var address string = "北京市昌平区天通苑"
address[0]='d'
fmt.Println("address=",address)
}
demo03
package main //hello.go所在的包的是main
import "fmt" //引入一个包 'fmt'
func main() {
//双引号能识别转义字符
var address string = "北京市\n昌平区天通苑"
fmt.Println("address=",address)
}
demo04
package main //hello.go所在的包的是main
import "fmt" //引入一个包 'fmt'
func main() {
//反引号能输出源生格式
var helloworld string = `
package main //hello.go所在的包的是main
import "fmt" //引入一个包 'fmt'
func main() {
fmt.Println("hello,world") //调用fmt包的函数Println输出"hello,world"
}
`
fmt.Println(helloworld)
}
demo05
package main //hello.go所在的包的是main
import "fmt" //引入一个包 'fmt'
func main() {
//字符串拼接
var helloworld string = "hello " + "world"
fmt.Println(helloworld)
}
demo06
package main //hello.go所在的包的是main
import "fmt" //引入一个包 'fmt'
func main() {
//字符串拼接很长时,可以分行写,因为go自动加";",所以+放在每行结尾
var helloworld string = "hello " + "world " + "hello " + "world " +
"hello " + "world " + "hello " + "world " + "hello " + "world "+
"hello " + "world "
fmt.Println(helloworld)
}