如何在反引号字符串中插入一个反引号?

huangapple go评论84阅读模式
英文:

how to put a backquote in a backquoted string?

问题

在Go语言中,是否可以使用反引号打印反引号,就像这样:

package main

import "fmt"

func main() {
    fmt.Println(```) // 例如,我可以使用双引号"\`"来实现
}
英文:

is it possible to print back quotes in Go using back quotes : something like this:

package main

import "fmt"

func main() {
	fmt.Println(```) // for example I can do it with double quotes "\""
}

答案1

得分: 28

package main

import "fmt"

func main() {
// back quote fmt.Println((back + "" + quote))
}

原始字符串字面量是在反引号``之间的字符序列。
在引号内,除了反引号之外,任何字符都是合法的。
原始字符串字面量的值是在引号之间的未解释的字符组成的字符串;
特别地,反斜杠没有特殊含义,字符串可以跨越多行。字符串字面量

英文:
package main

import "fmt"

func main() {
	// back ` quote
	fmt.Println((`back ` + "`" + ` quote`))
}

> Raw string literals are character
> sequences between back quotes ``.
> Within the quotes, any character is
> legal except back quote. The value of
> a raw string literal is the string
> composed of the uninterpreted
> characters between the quotes; in
> particular, backslashes have no
> special meaning and the string may
> span multiple lines. String
> literals

答案2

得分: 1

你也可以使用单引号来完成:

package main
import "fmt"

func main() {
   fmt.Printf("%c\n", '`')
}

https://golang.org/pkg/fmt#Printf

英文:

You can also do it with single quotes:

package main
import "fmt"

func main() {
   fmt.Printf("%c\n", '`')
}

https://golang.org/pkg/fmt#Printf

答案3

得分: 0

TLDR

fmt.Println("\x60")

\x: 十六进制 <sup>参见 fmt</sup>

60<sub>16</sub>
96<sub>10</sub>
140<sub>8</sub> 匹配字符 ` <sup>重音符号</sup>


如何在反引号字符串中插入一个反引号?

英文:

TLDR

fmt.Println(&quot;\x60&quot;)

\x: Hex <sup>see fmt</sup>

60<sub>16</sub>
96<sub>10</sub>
140<sub>8</sub> matches the character ` <sup>grave accent</sup>


如何在反引号字符串中插入一个反引号?

huangapple
  • 本文由 发表于 2010年12月13日 04:48:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/4423951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定