将原始字符串文字用于多行字符串:
func main(){
multiline := `line
by line
and line
after line`
}
原始字符串文字
原始字符串文字是后引号之间的字符序列,如foo中所示 . 在引号内,除反向引号外,任何字符都可能出现 .
一个重要的部分是 raw literal不仅仅是多行而且多行并不是它的唯一目的 .
原始字符串文字的值是由引号之间的未解释(隐式UTF-8编码)字符组成的字符串;特别是,反斜杠没有特殊含义......
所以逃避不会被解释和 new lines between ticks will be real new lines .
func main(){
multiline := `line
by line \n
and line \n
after line`
// \n will be just printed.
// But new lines are there too.
fmt.Print(multiline)
}
连接
可能你想要打破你的长行,你不需要新行 . 在这种情况下,您可以使用字符串连接 .
func main(){
multiline := "line " +
"by line " +
"and line " +
"after line"
fmt.Print(multiline) // No new lines here
}
由于“”被解释,因此将解释字符串文字转义 .
func main(){
multiline := "line " +
"by line \n" +
"and line \n" +
"after line"
fmt.Print(multiline) // New lines as interpreted \n
}