Skip to content

Commit

Permalink
add space between Chinese and English characters by atom-pangs
Browse files Browse the repository at this point in the history
  • Loading branch information
bingohuang committed Aug 8, 2016
1 parent b37cd28 commit c3dd302
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions 01_Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

Go is a new language. Although it borrows ideas from existing languages, it has unusual properties that make effective Go programs different in character from programs written in its relatives. A straightforward translation of a C++ or Java program into Go is unlikely to produce a satisfactory result—Java programs are written in Java, not Go. On the other hand, thinking about the problem from a Go perspective could produce a successful but quite different program. In other words, to write Go well, it's important to understand its properties and idioms. It's also important to know the established conventions for programming in Go, such as naming, formatting, program construction, and so on, so that programs you write will be easy for other Go programmers to understand.

Go 是一门全新的语言。尽管它从既有的语言中借鉴了许多理念,但其与众不同的特性, 使得使用Go编程在本质上就不同于其它语言。将现有的C++或Java程序直译为Go 程序并不能令人满意——毕竟Java程序是用Java编写的,而不是Go。 另一方面,若从Go的角度去分析问题,你就能编写出同样可行但大不相同的程序。 换句话说,要想将Go程序写得好,就必须理解其特性和风格。了解命名、格式化、 程序结构等既定规则也同样重要,这样你编写的程序才能更容易被其他程序员所理解。
Go 是一门全新的语言。尽管它从既有的语言中借鉴了许多理念,但其与众不同的特性, 使得使用 Go 编程在本质上就不同于其它语言。将现有的 C++ 或 Java 程序直译为 Go 程序并不能令人满意——毕竟 Java 程序是用 Java 编写的,而不是 Go。 另一方面,若从 Go 的角度去分析问题,你就能编写出同样可行但大不相同的程序。 换句话说,要想将 Go 程序写得好,就必须理解其特性和风格。了解命名、格式化、 程序结构等既定规则也同样重要,这样你编写的程序才能更容易被其他程序员所理解。

This document gives tips for writing clear, idiomatic Go code. It augments the [language specification](https://go-zh.org/ref/spec), [the Tour of Go](https://tour.golang.org/), and [How to Write Go Code](https://go-zh.org/doc/code.html), all of which you should read first.

本文档就如何编写清晰、地道的Go代码提供了一些技巧。它是对[语言规范](https://go-zh.org/ref/spec)[Go语言之旅](https://tour.golang.org/)以及[如何使用Go编程](https://go-zh.org/doc/code.html)的补充说明,因此我们建议您先阅读这些文档。
本文档就如何编写清晰、地道的 Go 代码提供了一些技巧。它是对 [语言规范](https://go-zh.org/ref/spec)[Go 语言之旅](https://tour.golang.org/) 以及 [如何使用 Go 编程](https://go-zh.org/doc/code.html) 的补充说明,因此我们建议您先阅读这些文档。

### Examples

### 示例

The [Go package sources](https://golang.org/src/) are intended to serve not only as the core library but also as examples of how to use the language. Moreover, many of the packages contain working, self-contained executable examples you can run directly from the [golang.org](https://golang.org/) web site, such as [this one](https://golang.org/pkg/strings/#example_Map) (if necessary, click on the word "Example" to open it up). If you have a question about how to approach a problem or how something might be implemented, the documentation, code and examples in the library can provide answers, ideas and background.
The [Go package sources](https://golang.org/src/) are intended to serve not only as the core library but also as examples of how to use the language. Moreover, many of the packages contain working, self-contained executable examples you can run directly from the [golang.org](https://golang.org/) web site, such as [this one](https://golang.org/pkg/strings/#example_Map) (if necessary, click on the word"Example"to open it up). If you have a question about how to approach a problem or how something might be implemented, the documentation, code and examples in the library can provide answers, ideas and background.

[Go包的源码](https://go-zh.org/src/pkg/)不仅是核心库,同时也是学习如何使用Go语言的示例源码。 此外,其中的一些包还包含了可工作的,独立的可执行示例,你可以直接在 [golang.org](https://golang.org/) 网站上运行它们,比如 [这个例子](https://golang.org/pkg/strings/#example_Map) (单击文字“示例”来展开它)。如果你有任何关于某些问题如何解决,或某些东西如何实现的疑问, 也可以从中获取相关的答案、思路以及后台实现。
[Go 包的源码](https://go-zh.org/src/pkg/) 不仅是核心库,同时也是学习如何使用 Go 语言的示例源码。 此外,其中的一些包还包含了可工作的,独立的可执行示例,你可以直接在 [golang.org](https://golang.org/) 网站上运行它们,比如 [这个例子](https://golang.org/pkg/strings/#example_Map) (单击文字 “示例” 来展开它)。如果你有任何关于某些问题如何解决,或某些东西如何实现的疑问, 也可以从中获取相关的答案、思路以及后台实现。
8 changes: 4 additions & 4 deletions 02_Formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Formatting issues are the most contentious but the least consequential. People c

With Go we take an unusual approach and let the machine take care of most formatting issues. The gofmt program (also available as go fmt, which operates at the package level rather than source file level) reads a Go program and emits the source in a standard style of indentation and vertical alignment, retaining and if necessary reformatting comments. If you want to know how to handle some new layout situation, run gofmt; if the answer doesn't seem right, rearrange your program (or file a bug about gofmt), don't work around it.

在Go中我们另辟蹊径,让机器来处理大部分的格式化问题。gofmt 程序(也可用 go fmt,它以包为处理对象而非源文件)将Go程序按照标准风格缩进、 对齐,保留注释并在需要时重新格式化。若你想知道如何处理一些新的代码布局,请尝试运行 gofmt;若结果仍不尽人意,请重新组织你的程序(或提交有关 gofmt 的Bug),而不必为此纠结。
在 Go 中我们另辟蹊径,让机器来处理大部分的格式化问题。gofmt 程序(也可用 go fmt,它以包为处理对象而非源文件)将 Go 程序按照标准风格缩进、 对齐,保留注释并在需要时重新格式化。若你想知道如何处理一些新的代码布局,请尝试运行 gofmt;若结果仍不尽人意,请重新组织你的程序(或提交有关 gofmt 的 Bug),而不必为此纠结。

As an example, there's no need to spend time lining up the comments on the fields of a structure. Gofmt will do that for you. Given the declaration

Expand Down Expand Up @@ -46,7 +46,7 @@ type T struct {

All Go code in the standard packages has been formatted with gofmt.

标准包中所有的Go代码都已经用 gofmt 格式化过了。
标准包中所有的 Go 代码都已经用 gofmt 格式化过了。

Some formatting details remain. Very briefly:

Expand All @@ -66,9 +66,9 @@ Parentheses
缩进
我们使用制表符(tab)缩进,gofmt 默认也使用它。在你认为确实有必要时再使用空格。
行的长度
Go对行的长度没有限制,别担心打孔纸不够长。如果一行实在太长,也可进行折行并插入适当的tab缩进
Go 对行的长度没有限制,别担心打孔纸不够长。如果一行实在太长,也可进行折行并插入适当的 tab 缩进
括号
比起C和Java,Go所需的括号更少:控制结构(if、for 和 switch)在语法上并不需要圆括号。此外,操作符优先级处理变得更加简洁,因此
比起 C 和 Java,Go 所需的括号更少:控制结构(if、for 和 switch)在语法上并不需要圆括号。此外,操作符优先级处理变得更加简洁,因此
x<<8 + y<<16
正表述了空格符所传达的含义。
```
12 changes: 6 additions & 6 deletions 03_Commentary.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

Go provides C-style `/* */` block comments and C++-style `//` line comments. Line comments are the norm; block comments appear mostly as package comments, but are useful within an expression or to disable large swaths of code.

Go语言支持C风格的块注释 `/* */` 和C++风格的行注释 `//`。 行注释更为常用,而块注释则主要用作包的注释,当然也可在禁用一大段代码时使用。
Go 语言支持 C 风格的块注释 `/* */` 和 C++ 风格的行注释 `//`。 行注释更为常用,而块注释则主要用作包的注释,当然也可在禁用一大段代码时使用。

The program—and web server—godoc processes Go source files to extract documentation about the contents of the package. Comments that appear before top-level declarations, with no intervening newlines, are extracted along with the declaration to serve as explanatory text for the item. The nature and style of these comments determines the quality of the documentation godoc produces.

godoc 既是一个程序,又是一个Web服务器,它对Go的源码进行处理,并提取包中的文档内容。 出现在顶级声明之前,且与该声明之间没有空行的注释,将与该声明一起被提取出来,作为该条目的说明文档。 这些注释的类型和风格决定了 godoc 生成的文档质量。
godoc 既是一个程序,又是一个 Web 服务器,它对 Go 的源码进行处理,并提取包中的文档内容。 出现在顶级声明之前,且与该声明之间没有空行的注释,将与该声明一起被提取出来,作为该条目的说明文档。 这些注释的类型和风格决定了 godoc 生成的文档质量。

Every package should have a package comment, a block comment preceding the package clause. For multi-file packages, the package comment only needs to be present in one file, and any one will do. The package comment should introduce the package and provide information relevant to the package as a whole. It will appear first on the godoc page and should set up the detailed documentation that follows.

Expand Down Expand Up @@ -71,7 +71,7 @@ If the package is simple, the package comment can be brief.
```
Comments do not need extra formatting such as banners of stars. The generated output may not even be presented in a fixed-width font, so don't depend on spacing for alignment—godoc, like gofmt, takes care of that. The comments are uninterpreted plain text, so HTML and other annotations such as `_this_` will reproduce _verbatim_ and should not be used. One adjustment godoc does do is to display indented text in a fixed-width font, suitable for program snippets. The package comment for the [fmt package](https://go-zh.org/pkg/fmt/) uses this to good effect.

注释无需进行额外的格式化,如用星号来突出等。生成的输出甚至可能无法以等宽字体显示, 因此不要依赖于空格对齐,godoc 会像 gofmt 那样处理好这一切。 注释是不会被解析的纯文本,因此像HTML或其它类似于 `_这样_` 的东西将按照 _原样_ 输出,因此不应使用它们。godoc 所做的调整, 就是将已缩进的文本以等宽字体显示,来适应对应的程序片段。 [fmt 包](https://go-zh.org/pkg/fmt/) 的注释就用了这种不错的效果。
注释无需进行额外的格式化,如用星号来突出等。生成的输出甚至可能无法以等宽字体显示, 因此不要依赖于空格对齐,godoc 会像 gofmt 那样处理好这一切。 注释是不会被解析的纯文本,因此像 HTML 或其它类似于 `_这样_` 的东西将按照 _原样_ 输出,因此不应使用它们。godoc 所做的调整, 就是将已缩进的文本以等宽字体显示,来适应对应的程序片段。 [fmt 包](https://go-zh.org/pkg/fmt/) 的注释就用了这种不错的效果。

Depending on the context, godoc might not even reformat comments, so make sure they look good straight up: use correct spelling, punctuation, and sentence structure, fold long lines, and so on.

Expand All @@ -94,16 +94,16 @@ func Compile(str string) (regexp *Regexp, err error) {
// Compile 用于解析正则表达式并返回,如果成功,则 Regexp 对象就可用于匹配所针对的文本。
func Compile(str string) (regexp *Regexp, err error) {
```
If the name always begins the comment, the output of godoc can usefully be run through grep. Imagine you couldn't remember the name "Compile" but were looking for the parsing function for regular expressions, so you ran the command,
If the name always begins the comment, the output of godoc can usefully be run through grep. Imagine you couldn't remember the name"Compile" but were looking for the parsing function for regular expressions, so you ran the command,
若注释总是以名称开头,godoc 的输出就能通过 grep 变得更加有用。假如你记不住“Compile”这个名称,而又在找正则表达式的解析函数, 那就可以运行
若注释总是以名称开头,godoc 的输出就能通过 grep 变得更加有用。假如你记不住 “Compile” 这个名称,而又在找正则表达式的解析函数, 那就可以运行
```go
$ godoc regexp | grep parse
```
If all the doc comments in the package began, "This function...", grep wouldn't help you remember the name. But because the package starts each doc comment with the name, you'd see something like this, which recalls the word you're looking for.
若包中的所有文档注释都以“此函数…”开头,grep 就无法帮你记住此名称。 但由于每个包的文档注释都以其名称开头,你就能看到这样的内容,它能显示你正在寻找的词语。
若包中的所有文档注释都以 “此函数…” 开头,grep 就无法帮你记住此名称。 但由于每个包的文档注释都以其名称开头,你就能看到这样的内容,它能显示你正在寻找的词语。
```go
$ godoc regexp | grep parse
Expand Down
Loading

0 comments on commit c3dd302

Please sign in to comment.