Comment on page

2010-03-31 Go 编程语言入门教程

原文:http://golang.org 翻译:刘金雨 http://log4think.com

介绍

本文档是关于Go编程语言基础的一个介绍性的入门教程,偏向于熟悉C或C++的读者。本文并非一份语言的完整指南,如果需要的话可以参考语言规范。读完本教程之后,你可以继续学习Effective Go,这份文档会更深入的挖掘如何使用Go语言。
此外还有一份《三日入门》的教程可供参考:
本文将会以一系列适当的程序来说明语言的一些关键特性。所有的示例程序都是可运行的(在撰写本文时),并且这些程序都会提交到版本库的
/doc/progs/
目录下。
程序片段都会标注上在源文件中的行号,为了清晰起见,空行前面的行号留空。

Hello World

先从一个最常见的开始:
05 package main
07 import fmt "fmt" // 本包实现了格式化输入输出
09 func main() {
10 fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界n");
11 }
每份Go的源文件都会使用
package
语句声明它的包名。同时也可以通过导入其它包来使用其中定义的功能。这段代码导入了包
fmt
来调用我们的老朋友--现在它的开头字母是大写的,并且前面带有包名限定
fmt.Printf
函数的声明使用关键字func,整个程序将会从为
main
包中的
main
函数开始(经过初始化之后)。
字符串常量可以包含Unicode字符,采用UTF-8编码(事实上,所有Go程序的源文件都是使用UTF-8编码的)。
注释的方式同C++一样:
/* ... */
// ...
稍后我们会继续提到
print

编译

Go是一个编译型语言。目前有两个编译器,其中
gccgo
编译器采用了GCC作为后端,此外还有一系列根据其所适用的架构命名的编译器,例如
6g
适用于64位的x86结构,8g 适用于32位的x86结构,等等。这些编译器比gccgo运行的更快、生成的代码更加有效率。在撰写本文的时候(2009年底),他们还具有一个更加健壮的运行期系统,但是gccgo也正在迎头赶上。
下面来看看如何编译和运行程序。采用
6g
是这样的
$ 6g helloworld.go 编译; 中间代码位于 helloworld.6 中
$ 6l helloworld.6 链接; 输出至 6.out
$ 6.out
Hello, world; or Καλημέρα κόσμε; or こんにちは 世界
$
gccgo
的方式看起来更加传统一些。
$ gccgo helloworld.go
$ a.out
Hello, world; or Καλημέρα κόσμε; or こんにちは 世界
$

Echo

下一步,来实现一个Unix的传统命令Echo:
05 package main
07 import (
08 "os";
09 "flag"; // command line option parser
10 )
12 var omitNewline = flag.Bool("n", false, "don't print final newline")
14 const (
15 Space = " ";
16 Newline = "n";
17 )
19 func main() {
20 flag.Parse(); // Scans the arg list and sets up flags
21 var s string = "";
22 for i := 0; i < flag.NArg(); i++ {
23 if i > 0 {
24 s += Space
25 }
26 s += flag.Arg(i);
27 }
28 if !*omitNewline {
29 s += Newline
30 }
31 os.Stdout.WriteString(s);
32 }
这段程序很小,但是却有几个新出现的概念。前面这个例子中,我们看到可以使用
func
来声明一个函数,同时关键字
var、const
type
目前还没有用到)也可以用于声明,就好像
import
一样。
注意,我们可以将同一类的声明放到括号中,以分号分隔。例如第7-10行和第14-17行。但也并非一定要如此,例如可以这样写
const Space = " "
const Newline = "n"
分号在此处并不是必须的。事实上,任何顶层声明后面都不需要分号。但如果要是在一个括号内进行一系列的声明,就需要用分号来分割了。
你可以像在C、C++或Java中那样去使用分号,但如果你愿意的话,在很多情况下都可以省略掉分号。分号是用于表示语句间的**分隔**,而非表示其**中止**。因此,对于一个代码块中的最后一条语句来说,有无分号皆可。大括号之后的分号也是可选的,就像C语言中的一样。
比对一下
echo
的源代码,只有第8、15和21行必须要加分号,当然第22行中的
for
语句中为了分隔三个表达式也需要加分号。第9、16、26和31行的分号都不是必须的,加上分号只是为了以后再增加语句的时候方便而已。
这个程序导入了os包以访问
Stdout
变量,
Stdout
的类型是
*os.File
import
语句实际上是个声明:通常情况下(如hello world程序中那样),它声明了一个标识符
fmt
用于访问导入的包的成员变量,而包是从当前目录或标准库下的
fmt
文件中导入的。在这个程序中,我们为导入的包显式的指定了一个名字,默认情况下,包名是采用在导入的包里面已经定义好的名字,通常会与文件名一致。因此在这个"hello world"程序中,可以只写
import "fmt"
。你可以任意为包指定一个导入名,但通常只有在解决名字冲突的情况下才有必要这样做。
有了
os.Stdout
,我们就可以用它的
WriteString
方法打印字符串了。
导入了
实 际flag
包之后,第12行创建了一个全局变量来保存 echo 的
-n
选项标志。
omitNewline
变量的类型是 *bool --指向bool值的指针。
main.main
中进行了参数解析,并创建了一个本地字符串类型的变量用于构造输出的内容。声明语句如下
var s string = "";
这里用到了关键字
var
,后面跟变量名和数据类型,之后可以继续接=来赋初值。
Go试图尽量保持简洁,这个声明也可以用更短的形式。因为初值是一个字符串类型的常量,没有必要再声明数据类型了,因此这个声明可以写成这样:
var s = "";
或者也可以直接用更短的形式:
s := "";
操作符
:=
在 Go 语言里经常会用在赋初值的声明中,比如下面这个
for
语句的声明:
22 for i := 0; i < flag.NArg(); i++ {
flag
包会解析命令行参数,并将参数值保存在一个列表中。
Go语言中的
for
语句和C语言中的有几个不同之处。首先,for是唯一的循环语句,没有
while
语句或
do
语句。其次,for语句后面的三个子句不需要圆括号,但大括号是必须的。这一条对
if
switch
语句同样适用。稍后还会有几个例子演示
for
语句的其它用法。
循环体中通过追加(+=)标志和空格构造字符串
s
。循环之后,如果没有设置
-n
标志,程序追加一个空行,最后输出结果。
注意,函数
main.main
没有返回值。它就是这样定义的,如果到达了
main.main
的末尾就表示"成功",如果想表明出错并返回,可以调用
os.Exit(1)
os
包还包含一些其他的常用功能,例如
os.Args
会被
flag
包用于访问命令行参数。
## 插播:数据类型 Types
Go支持一些常见的数据类型,例如
int
float
,其值采用机器"适用"的大小来表示。也有定义了明确大小的数据类型,例如
int8
float64
等,以及无符号整数类型,例如
uint
uint32
等。这些都是完全不同的数据类型,即使
int
int32
都是32位整数,但它们是不同的类型。对于表示字符串元素的类型
byte
uint8
也是同样如此。
说到字符串(
string
),这也是一个内置的数据类型。字符串的值不仅仅是一个
byte
的数组,它的值是**不可改变**的。一旦确定了一个字符串的值,就不能再修改了。但一个字符串**变量**的值可以通过重新赋值来改变。下面这段来自
strings.go
的代码是合法的:
11 s := "hello";
12 if s[1] != 'e' { os.Exit(1) }
13 s = "good bye";
14 var p *string = &s;
15 p = "ciao";
然而下面这段代码是非法的,因为它试图修改一个字符串的值:
s[0] = 'x';
(*p)[1] = 'y';
按照C++的说法,Go的字符串有点类似带了
const
修饰符,指向字符串的指针也类似于一个 const 字符串的引用(reference).
没错,前面看到的那些是指针,然而Go语言中的指针在用法方面有所简化,后文会提到。
数组的声明如下所示:
var arrayOfInt [10]int;
数组同字符串一样是"值",但是却是可变的。与C不同的是,C语言中
arrayOfInt
可以当做一个指向int的指针来用。在Go中,因为数组是**值**,因此
arrayOfInt
被看做(也被用做)指向数组的指针。
数组的大小是其数据类型的一部分。但是,你可以声明一个**slice**变量,然后可以用一个指向具有相同元素类型的数组指针给它赋值,更常见的是用一个形式为
a[low : high]
的**slice**表达式,该表达式表示下标从
low
high-1
的子数组。 Slice 类型类似数组,但没有显式指定大小(
[]
之于
[10]
),用于表示一个隐性(通常是匿名的)数组。如果不同的 slice 都是表示同一个数组中的数据,它们可以共享该数组的内存,但不同的数组则永远不会共享内存数据。
Slice 在 Go 程序中比数组更常见。它更灵活,并且具有引用的语义,效率也更高。其不足之处在于无法像数组一样精确控制存储方式,如果想在一个数据结构中保存一个具有 100个元素的序列,应该采用数组。
当给函数传一个数组参数的时候,绝大多数情况下都会把参数声明为 slice 类型。当调用函数时,先取数组地址,然后Go会创建一个 slice 的引用,然后传这个引用过去。
可以用 slice 来写这个函数(来自
sum.go
):
09 func sum(a []int) int { // 返回一个整数
10 s := 0;
11 for i := 0; i < len(a); i++ {
12 s += a[i]
13 }
14 return s
15 }
之后这样来调用:
19 s := sum(&[3]int{1,2,3}); // a slice of the array is passed to sum
注意在
sum()
的参数列表后面加 int 定义了其返回值类型(int)。
[3]int{1,2,3}
的形式是一个数据类型后面接一个大括号括起来的表达式,整个这个表达式构造出了一个值,这里是一个包含三个整数的数组。前面的
&
表示提取这个值的地址。这个地址会被隐性的转为一个 slice 传给
sum()
如果想创建一个数组,但希望编译器来帮你确定数组的大小,可以用
...
作为数组大小:
s := sum(&[...]int{1,2,3});
实际使用中,除非非常在意数据结构的存储方式,否则 slice 本身 (用[]且不带
&
) 就足够了:
s := sum([]int{1,2,3});
除此之外还有map,可以这样初始化:
m := map[string]int{"one":1 , "two":2}
sum还第一次出现 了
内置函数
len(),用于返回元素数量。
可以用于字符串、数组、slice、map、map和channel.
此外,
for
循环中的
range
也可以用于字符串、数组、slice、map、map和channel。例如
for i := 0; i < len(a); i++ { ... }
遍历一个序列的每个元素,可以写成
for i, v := range a { ... }
其中, i 会赋值为下标, v 会赋值为 a 中对应的值,[Effective Go](http://golang.org/doc/effective_go.html)中包含了更多的用法演示。
## An Interlude about Allocation
Go中的大多数数据类型都是值类型。对
int
struct
或数组的赋值会拷贝其内容。
new()
可以分配一个新的变量,并返回其分配的存储空间的地址。例如
type T struct { a, b int }
var t *T = new(T);
或者更常见的写法:
t := new(T);
Some types-maps, slices, and channels (see below)-have reference semantics. If you're holding a slice or a map and you modify its contents, other variables referencing the same underlying data will see the modification. For these three types you want to use the built-in function
make()
:
m := make(map[string]int);
This statement initializes a new map ready to store entries. If you just declare the map, as in
var m map[string]int;
it creates a
nil
reference that cannot hold anything. To use the map, you must first initialize the reference using
make()
or by assignment from an existing map.
Note that
new(T)
returns type
*T
while
make(T)
returns type
T
. If you (mistakenly) allocate a reference object with
new()
, you receive a pointer to a nil reference, equivalent to declaring an uninitialized variable and taking its address.
## An Interlude about Constants
Although integers come in lots of sizes in Go, integer constants do not. There are no constants like
0LL
or
0x0UL
. Instead, integer constants are evaluated as large-precision values that can overflow only when they are assigned to an integer variable with too little precision to represent the value.
const hardEight = (1 << 100) >> 97 // legal
There are nuances that deserve redirection to the legalese of the language specification but here are some illustrative examples:
var a uint64 = 0 // a has type uint64, value 0
a := uint64(0) // equivalent; uses a "conversion"
i := 0x1234 // i gets default type: int
var j int = 1e6 // legal - 1000000 is representable in an int
x := 1.5 // a float
i3div2 := 3/2 // integer division - result is 1
f3div2 := 3./2. // floating point division - result is 1.5
Conversions only work for simple cases such as converting
ints
of one sign or size to another, and between
ints
and
floats
, plus a few other simple cases. There are no automatic numeric conversions of any kind in Go, other than that of making constants have concrete size and type when assigned to a variable.
## An I/O Package
Next we'll look at a simple package for doing file I/O with the usual sort of open/close/read/write interface. Here's the start of
file.go
:
05 package file
07 import (
08 "os";
09 "syscall";
10 )
12 type File struct {
13 fd int; // file descriptor number
14 name string; // file name at Open time
15 }
The first few lines declare the name of the package-
file
-and then import two packages. The
os
package hides the differences between various operating systems to give a consistent view of files and so on; here we're going to use its error handling utilities and reproduce the rudiments of its file I/O.
The other item is the low-level, external
syscall
package, which provides a primitive interface to the underlying operating system's calls.
Next is a type definition: the
type
keyword introduces a type declaration, in this case a data structure called
File
. To make things a little more interesting, our
File
includes the name of the file that the file descriptor refers to.
Because
File
starts with a capital letter, the type is available outside the package, that is, by users of the package. In Go the rule about visibility of information is simple: if a name (of a top-level type, function, method, constant or variable, or of a structure field or method) is capitalized, users of the package may see it. Otherwise, the name and hence the thing being named is visible only inside the package in which it is declared. This is more than a convention; the rule is enforced by the compiler. In Go, the term for publicly visible names is ''exported''.
In the case of
File
, all its fields are lower case and so invisible to users, but we will soon give it some exported, upper-case methods.
First, though, here is a factory to create a
File
:
17 func newFile(fd int, name string) *File {
18 if fd < 0 {
19 return nil
20 }
21 return &File{fd, name}
22 }
This returns a pointer to a new
File
structure with the file descriptor and name filled in. This code uses Go's notion of a ''composite literal'', analogous to the ones used to build maps and arrays, to construct a new heap-allocated object. We could write
n := new(File);
n.fd = fd;
n.name = name;
return n
but for simple structures like
File
it's easier to return the address of a nonce composite literal, as is done here on line 21.
We can use the factory to construct some familiar, exported variables of type
*File
:
24 var (
25 Stdin = newFile(0, "/dev/stdin");
26 Stdout = newFile(1, "/dev/stdout");
27 Stderr = newFile(2, "/dev/stderr");
28 )
The
newFile
function was not exported because it's internal. The proper, exported factory to use is
Open
:
30 func Open(name string, mode int, perm int) (file *File, err os.Error) {
31 r, e := syscall.Open(name, mode, perm);
32 if e != 0 {
33 err = os.Errno(e);
34 }
35 return newFile(r, name), err
36 }
There are a number of new things in these few lines. First,
Open
returns multiple values, a
File
and an error (more about errors in a moment). We declare the multi-value return as a parenthesized list of declarations; syntactically they look just like a second parameter list. The function
syscall.Open
also has a multi-value return, which we can grab with the multi-variable declaration on line 31; it declares
r
and
e
to hold the two values, both of type
int
(although you'd have to look at the
syscall
package to see that). Finally, line 35 returns two values: a pointer to the new
File
and the error. If
syscall.Open
fails, the file descriptor
r
will be negative and
NewFile
will return
nil
.
About those errors: The
os
library includes a general notion of an error. It's a good idea to use its facility in your own interfaces, as we do here, for consistent error handling throughout Go code. In
Open
we use a conversion to translate Unix's integer
errno
value into the integer type
os.Errno
, which implements
os.Error
.
Now that we can build
Files
, we can write methods for them. To declare a method of a type, we define a function to have an explicit receiver of that type, placed in parentheses before the function name. Here are some methods for
*File
, each of which declares a receiver variable
file
.
38 func (file *File) Close() os.Error {
39 if file == nil {
40 return os.EINVAL
41 }
42 e := syscall.Close(file.fd);
43 file.fd = -1; // so it can't be closed again
44 if e != 0 {
45 return os.Errno(e);
46 }
47 return nil
48 }
50 func (file *File) Read(b []byte) (ret int, err os.Error) {
51 if file == nil {
52 return -1, os.EINVAL
53 }
54 r, e := syscall.Read(file.fd, b);
55 if e != 0 {
56 err = os.Errno(e);
57 }
58 return int(r), err
59 }
61 func (file *File) Write(b []byte) (ret int, err os.Error) {
62 if file == nil {
63 return -1, os.EINVAL
64 }
65 r, e := syscall.Write(file.fd, b);
66 if e != 0 {
67 err = os.Errno(e);
68 }
69 return int(r), err
70 }
72 func (file *File) String() string {
73 return file.name
74 }
There is no implicit
this
and the receiver variable must be used to access members of the structure. Methods are not declared within the
struct
declaration itself. The
struct
declaration defines only data members. In fact, methods can be created for almost any type you name, such as an integer or array, not just for
structs
. We'll see an example with arrays later.
The
String
method is so called because of a printing convention we'll describe later.
The methods use the public variable
os.EINVAL
to return the (
os.Error
version of the) Unix error code
EINVAL
. The
os
library defines a standard set of such error values.
We can now use our new package:
05 package main
07 import (
08 "./file";
09 "fmt";
10 "os";
11 )
13 func main() {
14 hello := []byte{'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', 'n'};
15 file.Stdout.Write(hello);
16 file, err := file.Open("/does/not/exist", 0, 0);
17 if file == nil {
18 fmt.Printf("can't open file; err=%sn", err.String());
19 os.Exit(1);
20 }
21 }
The ''
./
'' in the import of ''
./file
'' tells the compiler to use our own package rather than something from the directory of installed packages.
Finally we can run the program:
% helloworld3
hello, world
can't open file; err=No such file or directory
%
## Rotting cats
Building on the
file
package, here's a simple version of the Unix utility
cat(1)
,
progs/cat.go
:
05 package main
07 import (
08 "./file"
09 "flag"
10 "fmt"
11 "os"
12 )
14 func cat(f *file.File) {
15 const NBUF = 512
16 var buf [NBUF]byte
17 for {
18 switch nr, er := f.Read(&buf); true {
19 case nr < 0:
20 fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", f.String(), er.String())
21 os.Exit(1)
22 case nr == 0: // EOF
23 return
24 case nr > 0:
25 if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
26 fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String())
27 }
28 }
29 }
30 }
32 func main() {
33 flag.Parse() // Scans the arg list and sets up flags
34 if flag.NArg() == 0 {
35 cat(file.Stdin)
36 }
37 for i := 0; i < flag.NArg(); i++ {
38 f, err := file.Open(flag.Arg(i), 0, 0)
39 if f == nil {
40 fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
41 os.Exit(1)
42 }