在 Golang 中,可以使用反引号 `
来定义原始字符串(raw string),类似于 Python 中的三个单引号或三个双引号。原始字符串中的转义字符不会被解释,包括换行符和特殊字符。
以下是一个示例:
package main
import "fmt"
func main() {
s := `
This is a raw string.
It can contain multiple lines and special characters like \n and \t.
There is no need to escape them.
`
fmt.Println(s)
}
运行结果为:
This is a raw string.
It can contain multiple lines and special characters like \n and \t.
There is no need to escape them.
需要注意的是,在原始字符串中包含反引号时,需要使用双反斜杠进行转义。
除了原始字符串之外,Golang 中还有普通字符串和字节串类型,它们分别用双引号和单引号表示。普通字符串中的转义字符会被解释,而字节串则表示为一系列字节值。