java多行字符串_你如何在Go中写多行字符串?

将原始字符串文字用于多行字符串:

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值