Golang的Semicolons
Semicolons
The formal grammar uses semicolons ";"
as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:
When the input is broken into tokens, a semicolon is automatically inserted into the token stream at the end of a non-blank line if the line's final token is
- an identifier
- an integer, floating-point, imaginary, rune, or string literal
- one of the keywords
break
,continue
,fallthrough
, orreturn
- one of the operators and delimiters
++
,--
,)
,]
, or}
- To allow complex statements to occupy a single line, a semicolon may be omitted before a closing
")"
or"}"
.
To reflect idiomatic use, code examples in this document elide semicolons using these rules.
说得很清楚:
Golang编译器自动在行尾插入分号(Semicolons):
1. 关键字keywords
2. 标识符identifiers
3. 直接量Literals
4. 某些运算符: ++, --, ), ], }
由于在初始化块{...}中使用逗号而不是分号, 所以在块内换行需要谨慎编译器会自动插入分号.
像
var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt","Gopher names:\nGeorge\nGeoffrey\nGonzo"}, {"todo.txt", "Get animal handling license."}
}
编译时会报错, 原因是在最后一个元素后自动插入了分号, 解决办法:
1. 在最后一个元素后也加逗号
var files = []struct { Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt","Gopher names:\nGeorge\nGeoffrey\nGonzo"}, {"todo.txt", "Get animal handling license."},
}
2. 最后"}"不换行
var files = []struct {
Name, Body string }{ {"readme.txt", "This archive contains some text files."}, {"gopher.txt", "Gophernames:\nGeorge\nGeoffrey\nGonzo"}, {"todo.txt", "Get animal handling license."}}
Golang的Semicolons的更多相关文章
- [转]50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs
http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/ 50 Shades of Go: Traps, Gotc ...
- Golang 学习资料
资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/li ...
- Golang教程:数组和切片
数组 数组是类型相同的元素的集合.例如,整数 5, 8, 9, 79, 76 的集合就构成了一个数组.Go不允许在数组中混合使用不同类型的元素(比如整数和字符串). 声明 var variable_n ...
- golang基础归纳
1. hello-world package main import "fmt" func main(){ fmt.Println("Hello world, Go Go ...
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- 说说Golang的使用心得
13年上半年接触了Golang,对Golang十分喜爱.现在是2015年,离春节还有几天,从开始学习到现在的一年半时间里,前前后后也用Golang写了些代码,其中包括业余时间的,也有产品项目中的.一直 ...
- TODO:Golang指针使用注意事项
TODO:Golang指针使用注意事项 先来看简单的例子1: 输出: 1 1 例子2: 输出: 1 3 例子1是使用值传递,Add方法不会做任何改变:例子2是使用指针传递,会改变地址,从而改变地址. ...
- Golang 编写的图片压缩程序,质量、尺寸压缩,批量、单张压缩
目录: 前序 效果图 简介 全部代码 前序: 接触 golang 不久,一直是边学边做,边总结,深深感到这门语言的魅力,等下要跟大家分享是最近项目 服务端 用到的图片压缩程序,我单独分离了出来,做成了 ...
- golang struct扩展函数参数命名警告
今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...
随机推荐
- c#中如何不通过后台直接用js筛选gridview中的数据条件筛选查询?
js: //条件筛选 var showstate = true; function imagechange() { if (showstate) { $('#_toggle').hide(500, f ...
- CSS3之阴影
CSS3中新增属性-阴影,可以做出很多漂亮的效果. 文字阴影text-shadow text-shadow属性值的顺序: text-shadow: h-shadow v-shadow blur col ...
- hdu2085
#include <stdio.h> int main(){ __int64 h[],l[]; int i,j; int n; ){ h[]=; l[]=; ;i<=n;++i){ ...
- Java 编译报错:illegal character
1.检查编译版本:1.5还是1.6 2.重新引用一下Jar包
- Android IOS WebRTC 音视频开发总结(十八)-- 手机适配
本文主要介绍上次碰到的某些机器上看不到视频的问题,文章来自博客园RTC.Blacker,转载请说明出处. 之前做的视频聊天App一直运行良好,前几天客户反馈说在三星9100. Android4.0.3 ...
- 较好的IOS新闻客户端应用源码
兼容性较好的新闻客户端应用源码,这个是一款国外新闻客户端源码,并且这款应用兼容性非常好的,可以很好地兼容iPhone和iPad的使用,而且应用的功能很多,新闻列表,上啦下拉刷新效果,评论列表,在线评论 ...
- WP8 调用特定API权限不足
1.在解决方案中依次打开 Properties -> WMAppManifest.xml 2.点击功能 3.在左侧功能列表中勾选想要的功能权限.完毕
- crontab的使用说明
网上瞎转载的,仅供参考 名称 : crontab 使用权限 : 所有使用者 使用方式 : crontab file [-u user]-用指定的文件替代目前的crontab. crontab-[-u ...
- POJ C程序设计进阶 编程题#3:运算符判定
编程题#3:运算符判定 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 两个 ...
- POJ C程序设计进阶 编程题#1:单词翻转
编程题#1:单词翻转 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一 ...