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 ...
随机推荐
- Gem5全系统模式下运行SPLASH-2 Benchmarks使用alpha ISA
Steps to run the SPLASH-2 Benchmarks on M5 in full system mode using the alpha ISA. This Guide is ai ...
- java 中的fanal
三.java中有final final 修饰符 关键字可用于修饰类,变量和方法,final关键字有点类似于C#里的sealed 关键字,用于表示它修饰的类,方法和变量不可改变. fina修饰变量时,表 ...
- 使用Apache CXF开发WebServices服务端、客户端
在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端. 但是如果你访问Apache的官网,可以看到xfire已经被合并了. 最新的框架叫做CXF. Apache CXF = C ...
- windows在远程桌面连接中使用命令行参数
在此版本的 Windows 中,可以从搜索框("运行"对话框或命令行)启动远程桌面连接,而不是从「开始」菜单启动它. 从"运行"对话框启动远程桌面的步骤 依次单 ...
- centos atomic host第一次启动
centos atomic host安装完成会,第一次启动时会调用cloud-init等服务.这是个什么东东? cloud-init用于在创建虚拟机时通过元数据服务对虚拟机基本配置,包括常见的主机名, ...
- linux常见驱动修改
=============================== 说明 ===============================本文以A5为例,举8种我们公司常用接口的极度精简的驱动程序,只宜参考 ...
- Sqlserver替换函数Replace
Sqlserver中Replace函数:实现字段中某个字符串批量替换. 注意:强烈建议替换前备份数据库以免发生灾难性后果. update article set [Content]=replace([ ...
- idea类似eclipse鼠标提示java api信息
<ignore_js_op> 详细说明:http://java.662p.com/thread-2615-1-1.html
- libharu 源码编译 VS2010
最近项目中接过了一个libharu PDF 开源库的锅,哈哈.于是就自己编译了一套libharu 的MFC下的静态库和动态库. 编译libharu需要用到zlib库和libpng库,libpng库又依 ...
- jQuery实现动态添加和删除一个div
本文主要给大家简单介绍一下如何动态的在一个元素添加和删除div,希望能够得到举一反三之效. 代码实例如下: <!DOCTYPE html> <html> <head> ...