欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Go语言中的反引号

程序员文章站 2022-05-21 20:38:17
...

 

看到grafana的源码,有个反引号,就查下具体的含义:

type jwtToken struct {
	ExpiresOn       time.Time `json:"-"`
	ExpiresOnString string    `json:"expires_on"`
	AccessToken     string    `json:"access_token"`
}

 

 

结论写在最前:在Go语言中不倾向于使用单引号来表示字符串,请根据需要使用双引号或反引号。


一个Go语言字符串是一个任意字节的常量序列。Go语言的字符串类型在本质上就与其他语言的字符串类型不同。Java的String、C++的std::string以及python3的str类型都只是定宽字符序列,而 Go语言的字符串是一个用UTF-8编码的变宽字符序列,它的每一个字符都用一个或多个字节表示 

Go语言中的字符串字面量使用 双引号 或 反引号 来创建 

  • 双引号用来创建 可解析的字符串字面量 (支持转义,但不能用来引用多行);
  • 反引号用来创建 原生的字符串字面量 ,这些字符串可能由多行组成(不支持任何转义序列),原生的字符串字面量多用于书写多行消息、HTML以及正则表达式

There are two forms: raw string literals and interpreted string literals.

  • Raw string literals are character sequences between back quotes, as in foo .
  • Interpreted string literals are character sequences between double quotes, as in “bar”.

A rune literal represents a rune constant, an integer value identifying a Unicode code point. A rune literal is expressed as one or more characters enclosed in single quotes, as in ‘x’ or ‘\n’. Within the quotes, any character may appear except newline and unescaped single quote. A single quoted character represents the Unicode value of the character itself, while multi-character sequences beginning with a backslash encode values in various formats.

 

参考:

 

https://studygolang.com/articles/7312