语法错误:意外的”son”,期望分号、换行符或”}”。

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

syntax error: unexpected son, expecting semicolon, newline, or }

问题

当我在json格式中使用普通键盘输入'时,出现了以下错误。

type Movie struct {
    Title   string
    Year    int     'json:"released"'
    Color   bool    'json:"color,omitempty"'
    Actors  []string
}

-go run * ----> :syntax error: unexpected son, expecting semicolon, newline, or }

然后,我从StackOverflow复制了"`"并替换为原始字符,如下所示:

type Movie struct {
    Title   string
    Year    int     `json:"released"`
    Color   bool    `json:"color,omitempty"`
    Actors  []string
}

然后,语法错误消失了 语法错误:意外的”son”,期望分号、换行符或”}”。

GO文件是否期望'作为Unicode字符,或者是否有任何设置可以更改这一点?

英文:

I got this error when I used normal keyboard for ' in json format as below.

type Movie struct {
Title 	string
Year 	int 	'json:"released"'
Color 	bool 	'json:"color,omitempty"'
Actors	[]string}

`-go run * ----> :syntax error: unexpected son, expecting semicolon, newline, or }

and, then, I copied " ` " from stackOverflow and replaced with the original ones as below

type Movie struct {
Title 	string
Year 	int 	`json:"released"`
Color 	bool 	`json:"color,omitempty"`
Actors	[]string}    

and, then, the syntax is gone 语法错误:意外的”son”,期望分号、换行符或”}”。
does GO file expect ' as unicode or is there any setting for that?

答案1

得分: 3

这两个是不同的字符:撇号(')和反引号(`)。Go语言使用反引号来进行结构体类型注释,也称为结构体标签。在你的例子中,它们被用来为encoding/json包注释JSON键名。请参考这个问题了解如何输入它们。

英文:

These two are distinct characters: the apostrophe (') and backtick (`). The Go language uses backticks for struct type annotations, also called struct tags. In your example, they were used to annotate JSON key names for the encoding/json package to use. See this question on how to input them.

huangapple
  • 本文由 发表于 2016年4月17日 20:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/36676413.html
匿名

发表评论

匿名网友

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

确定