为什么在关闭括号之前,Golang中的分号是可选的?

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

Why are golang semicolons optional before a close parenthesis?

问题

根据Go文档的说明,如果分号紧跟在)}之前,它是可选的。

为了允许复杂语句占据一行,可以在闭合的")"或"}"之前省略分号。

我理解}规则允许像下面这样的一行语句:

if x { return 1 }

但是规则的目的是什么?在闭合括号之前可以出现什么样的语句或其他分号相关的内容?

英文:

According to the Go documentation a semicolon is optional if it is just before a ) or }.

> To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

I understand how the } rule allows one liners such as the following:

if x { return 1 }

But what is the purpose of the ) rule? What kind of statement or other semicolon thing can appear just before a close parenthesis?

答案1

得分: 3

要查找在闭括号之前分号可以出现的位置,请在规范中搜索文本";" } ")"

示例:

import ("fmt";)
var (v int;)
const (c = 1;)
type (t []int;)
英文:

To find where semicolons can appear before a closing parenthesis,
search the specification for the text ";" } ")".

Examples:

import ( "fmt"; )
var ( v int; )
const ( c = 1; )
type ( t []int; )

huangapple
  • 本文由 发表于 2022年6月20日 05:24:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/72680428.html
匿名

发表评论

匿名网友

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

确定