Go语言基础学习
通过Go语言旅行,你会发现Go语言如此简洁,优美,灵活,让人一见倾心,再见倾城,三见倾国
Packages
包
Every Go program is made up of packages.
每个Go程序是由包组成的
Programs start running in package main.
程序从main包开始运行
This program is using the packages with import paths "fmt" and "math".
这个程序包导入路径 fmt 和 math
By convention, the package name is the same as the last element of the import path.
习惯上,包名和最后一个导入路径相同
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println("Happy", math.Pi, "Day")
}
输出:
Happy 3.141592653589793 Day
Imports
导入
This code groups the imports into a parenthesized, "factored" import statement. You can also write multiple import statements, like:
代码的导入用括号括起来导入元素,也可以写成多个导入语句,像下面:
import "fmt"
import "math"
but it's common to use the factored form to eliminate clutter.
但是一般都是使用导入元素的方式以免混乱
Exported names
输出名字
After importing a package, you can refer to the names it exports.
导入包之后你就可以引用你导入的包名了
In Go, a name is exported if it begins with a capital letter.
在Go语言中,如果导入的名字首字母大写
Foo is an exported name, as is FOO. The name foo is not exported.
Foo是导入名,FOO和foo都不是合法的输出
Run the code. Then rename math.pi to math.Pi and try it again.
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.pi)
}
输出:
prog.go:9: cannot refer to unexported name math.pi
prog.go:9: undefined: math.pi
改成math.Pi就OK了
Functions
函数
A function can take zero or more arguments.
一个函数可以有0到多个参数
In this example, add takes two parameters of type int.
在这个例子中,add函数有2个int类型参数
Notice that the type comes after the variable name.
注意类型在变量名后边
(For more about why types look the way they do, see the article on Go's declaration syntax.)
http://golang.org/doc/articles/gos_declaration_syntax.html
想了解更多为什么他们要这么做,可以看下面的文章
package main
import "fmt"
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(42, 13))
}
When two or more consecutive named function parameters share a type, you can omit the type from all but the last.
当两个或者更多连续的参数类型相同时,你可以省略前面的类型只写最后一个
In this example, we shortened
x int, y int
to
x , y int
A function can return any number of results.
一个函数可以返回任意个结果
This function returns two strings.
这个函数返回两个字符串
package main
import "fmt"
func swap(x, y string) (string, string) {
return y, x
}
func main() {
a, b := swap("hello", "world")
fmt.Println(a, b)
}
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.
函数携带参数,在Go中,函数能返回多个 结果参数,不只一个单独的值,可以对这些 结果参数像变量一样命名和操作
If the result parameters are named, a return statement without arguments returns the current values of the results.
如果命名了 结果参数,一个空的返回语句,返回结果的当前值
package main
import "fmt"
func split(sum int) (x, y int) {
x = sum * 4/9
y = sum - x
return
}
func main() {
fmt.Println(split(17))
}
输出:
7 10
Variables
变量
The var statement declares a list of variables; as in function argument lists, the type is last.
var语句声明一个变量列表,和函数参数列表一样,类型在最后
package main
import "fmt"
var x, y, z int
var c, python, java bool
func main() {
fmt.Println(x, y, z, c, python, java)
}
输出:
0 0 0 false false false
A var declaration can include initializers, one per variable.
变量声明能包含每个变量的初始化
If an initializer is present, the type can be omitted; the variable will take the type of the initializer.
如果变量初始化了,类型可以省略,变量能够根据初始化判断类型
package main
import "fmt"
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)
}
输出:
1 2 3 true false no!
Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.
在函数内部, := 短赋值语句能够通过隐式类型取代var声明
(Outside a function, every construct begins with a keyword and the := construct is not available.)
在函数外,每个构造函数以关键字和 :=是不可以的