中文版:http://go-tour-cn.appsp0t.com/#4
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Happy", math.Pi, "Day")
}
每个 Go 程序都是由包组成的。
程序运行的入口从包的 main
方法。
这个程序使用并导入了包 "fmt"
和 "math"
。
按惯例,包名与导入路径的最后一个目录一致。
这个代码用圆括号组合了导入,这是“factored”(分解因子)式导入声明。同样可以编写多个导入语句,例如:
import "fmt" import "math"
不过通常都会用 factored 格式来使代码工整。
在导入了一个包之后,就可以用其导出的名称来调用它。
在 Go 中,首字母大写的名称是被导出的。
Foo
和 FOO
都是被导出的名称。
名称 foo
是不会被导出的。
执行代码。然后将 math.pi
改为 math.Pi
再试着执行一下。
package main import "fmt" func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) }
函数可以有零个或多个参数。
在这个例子中,add
有两个 int
类型的参数。
注意类型声明在变量名之后。(个人认为这个可以理解)
(参考这篇关于 Go 语法定义的文章了解类型以这种形式出现的原因。)
func swap(x, y string) (string, string) { return y, x } func main() { a, b := swap("hello", "world") fmt.Println(a, b) }
函数可以返回任意数量的返回值。
这个函数返回了两个字符串。
func split(sum int) (x, y int) { x = sum * 4/9 y = sum - x return } func main() { fmt.Println(split(17)) }
Functions take parameters. In Go, functions can return multiple "result parameters", not just a single value. They can be named and act just like variables.
If the result parameters are named, a return
statement
without arguments returns the current values of the results.
函数有参数。在 Go 中,函数可以返回多个“结果参数”,而不仅仅是一个值。它们可以像变量那样被命名和使用。
如果命名了返回值的参数,一个没有参数的 return
语句,会将当前的值作为返回值返回。
Var:
var x, y, z int var c, python, java bool func main() { fmt.Println(x, y, z, c, python, java) }
var
语句声明了一个变量的列表;跟函数的参数列表一样,类型声明在最后面。
0 0 0 false false false
var x, y, z int = 1, 2, 3 var c, python, java = true, false, "no!" func main() { fmt.Println(x, y, z, c, python, java) }
变量声明时可以包含初始值,每个变量对应一个。
如果初始值是存在的,则可以省略类型声明;变量将从初始值中获得类型。
func main() { var x, y, z int = 1, 2, 3 c, python, java := true, false, "no!" fmt.Println(x, y, z, c, python, java) }
在一个函数里面,短赋值语句:=
可以用于替代 var
的隐式类型声明。
(:=
结构不能使用在函数外,函数外的每个语法块都必须以关键字开始。)
Constant:
const Pi = 3.14 func main() { const World = "世界" fmt.Println("Hello", World) fmt.Println("Happy", Pi, "Day") const Truth = true fmt.Println("Go rules?", Truth) }
Constants are declared like variables, but with
the const
keyword.
Constants can be character, string, boolean, or numeric values.
注意这个Println。
数值常量:
const ( Big = 1<<100 Small = Big>>99 ) func needInt(x int) int { return x*10 + 1 } func needFloat(x float64) float64 { return x*0.1 } func main() { fmt.Println(needInt(Small)) fmt.Println(needFloat(Small)) fmt.Println(needFloat(Big)) }
数值常量是高精度的值。
一个未指定类型的常量由上下文来决定其类型。
也尝试一下输出 needInt(Big)
吧 溢出
For:
func main() { sum := 0 for i := 0; i < 10; i++ { sum += i } fmt.Println(sum) }
Go
只有一种循环结构,for
循环。
基本的 for
循环看起来跟 C
或者 Java 中做的一样,除了没有了 ( )
之外(甚至强制不能使用它们),而 {
}
是必须的。
func main() { sum := 1 for ; sum < 1000; { sum += sum } fmt.Println(sum) }
As in C or Java, you can leave the pre and post statements empty.
跟 C 或者 Java 中一样,前置、后置条件可以为空。
func main() { sum := 1 for sum < 1000 { sum += sum } fmt.Println(sum) }
基于这一点,你也可以省略分号: C
的 while
循环在 Go
中也是用 for
实现。
func main() { for ; ; { } }
如果省略了循环条件,它会一直循环下去(译者:死循环或无限循环)。
func main() { for { } }
为了避免累赘,分号也可以省略,这样一个无限循环可以被简洁地表达。
if:
import ( "fmt" "math" ) func pow(x, n, lim float64) float64 { if v := math.Pow(x, n); v < lim { return v } else { fmt.Printf("%g >= %g\n", v, lim) } // can‘t use v here, though return lim } func main() { fmt.Println( pow(3, 2, 10), pow(3, 3, 20), ) }
在 if
的简短声明处定义的变量同样可以在对应的 else
块中使用。这点要特别注意。
Go 的基本类型有
bool string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte // alias for uint8 rune // alias for int32 // represents a Unicode code point float32 float64 complex64 complex128
A struct
is a collection of fields.
(And a type
declaration does what you‘d expect.)
一个结构体(struct
)就是一个成员变量的集合。
(而 type
定义跟其字面意思相符。)
type Vertex struct { X int Y int } func main() { fmt.Println(Vertex{1, 2}) }
Struct Fields(结构体成员变量)使用点号来访问。
func main(){ v :=Vertex{1,2} v.X=4 fmt.Println(v.X)
}
Go has pointers, but no pointer arithmetic.
Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent.
Go 有指针,但是没有指针运算。
结构体成员变量可以通过结构体指针来访问。通过指针的间接访问也是透明的。
type Vertex struct{ X int Y int } func main(){ p := Vertex{1,2} q :=&p q.X=1e9 fmt.Println(p) }
输出:{1000000000 2}
说明可以直接输出结构体。
Struct Literals(结构体文法)表示通过结构体成员变量的值作为列表来新分配一个结构体。
使用 Name:
语法可以仅列出部分字段。(字段名的顺序无关。)
特殊的前缀 &
构造了指向结构体文法的指针。
A struct literal denotes a newly allocated struct value by listing the values of its fields.
You can list just a subset of fields by using
the Name:
syntax. (And the order of named fields is
irrelevant.)
The special prefix &
constructs a pointer to a
struct literal.
type Vertex struct { X, Y int } var ( p = Vertex{1, 2} // has type Vertex q = &Vertex{1, 2} // has type *Vertex r = Vertex{X: 1} // Y:0 is implicit s = Vertex{} // X:0 and Y:0 ) func main() { fmt.Println(p, q, r, s) }
{1 2} &{1 2} {1 0} {0 0}
new
函数表达式 new(T)
分配了一个零初始化的 T
值,并返回指向它的指针。(感觉这个语法有点奇怪,()里面为类型,怎么初始化呢?
var t *T = new(T)
或
t := new(T)
The expression new(T)
allocates a
zeroed T
value and returns a pointer to it.
var t *T = new(T)
or
t := new(T)
type Vertex struct { X, Y int } func main() { v := new(Vertex) fmt.Println(v) v.X, v.Y = 11, 9 fmt.Println(v) }
map 映射键到值。
map 必须用 make
来创建(不是 new
);一个值为 nil
的
map 是空的,并且不能赋值。
A map maps keys to values.
Maps must be created
with make
(not new
) before use;
the nil
map is empty and cannot be assigned to.
type Vertex struct { Lat, Long float64 } var m map[string]Vertex func main() { m = make(map[string]Vertex) m["Bell Labs"] = Vertex{ 40.68433, 74.39967, } fmt.Println(m["Bell Labs"]) }
go官网教程A Tour of Go,布布扣,bubuko.com
原文:http://www.cnblogs.com/youxin/p/3595238.html