`

Go语言旅行一 A Tour of Go

    博客分类:
  • Go
go 
阅读更多
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.)
在函数外,每个构造函数以关键字和 :=是不可以的
分享到:
评论

相关推荐

    A Tour of C++, 2nd Edition

    A Tour of C++ (2nd Edition) (C++ In-Depth Series) By 作者: Bjarne Stroustrup ISBN-10 书号: 0134997832 ISBN-13 书号: 9780134997834 Edition 版本: 2 出版日期: 2018-07-09 pages 页数: 815 In A Tour of ...

    C++语言导学 A Tour of C++(C++之父写的入门书)

    书中几乎介绍了C++语言的全部核心功能和重要的标准库组件,以很短的篇幅将C++语言的主要特性呈现给读者,并给出一些关键示例,让读者用很短的时间就能对现代C++的概貌有一个清晰的了解,尤其是关于面向对象编程和...

    手工安装GO官方教程A Tour of Go.docx

    A Tour of Go是官方提供的一个关于go语言短小精悍的教程,覆盖了包、变量、函数、数据类型等基础知识,也包括了方法、接口、并发编程等高级内容,读完并通过其中的练习题,短时间即可入门GO了。 该教程是一个WEB服务...

    A tour of C++ 2nd

    In A Tour of C++, Second Edition, Bjarne Stroustrup, the creator of C++, describes what constitutes modern C++. This concise, self-contained guide covers most major language features and the major ...

    A Tour of C++

    A Tour of C++ 一本不错的学习C++的书

    A Tour of C++ 2nd

    A Tour of C++ 2nd ,c++学习资料

    [mirror] A Tour of Go.zip

    [mirror] A Tour of Go

    A Tour of TensorFlow

    Abstract—Deep learning is a branch of artificial intelligence employing deep neural network architectures that has significantly advanced the state-of-the-art in computer vision, speech recognition, ...

    A Tour of C++/ C++语言导学

    高清非扫描版,带书签。Bjarne Stroustrup。。。。。。

    Bjarne Stroustrup A Tour of C++ (2013).pdf

    Bjarne Stroustrup A Tour of C++

    Whirlwind Tour Of Python

    如果你懂一些编程知识,比如学过C,Java等语言开发工具,或者你是一位具有工程背景的研究者或者工作者,那么这本书将会快速带你走进python世界,这是初学python者最佳学习资料,本附件是书中的练习题等源代码。...

    A Guided Tour Of Mathematical Physics (By Roel Snieder, Department Of Geophysics, Utrecht Univers.pdf

    A Guided Tour Of Mathematical Physics By Roel Snieder

    C++语言导学.A Tour of C++.pdf

    书中几乎介绍了C++语言的全部核心功能和重要的标准库组件,以很短的篇幅将C++语言的主要特性呈现给读者,并给出一些关键示例,让读者用很短的时间就能对现代C++的概貌有一个清晰的了解,尤其是关于面向对象编程和...

    A wavelet tour of signal processing 3rd edition

    英文三版。 Mallet 的小波分析经典教程。全书13章800多页,非常详尽的介绍了小波分析原理和应用。很经典的教程。

    A Whirlwind Tour of Python

    对于想快速了解和使用python的人,这是一本非常好的入门书,与《python数据科学手册》为姊妹篇。

    新版图书 A.Tour.of.C++.2nd.Edition.2018.7

    Stroustrup presents the C++ features in the context ... The tour covers some extensions being made for C++20, such as concepts and modules, and ends with a discussion of the design and evolution of C++.

    A Wavelet Tour of Signal Processing 3rd Edition

    Mallat的《信号处理的小波导引》的第三版 经典程度不用多说 PDF版本

Global site tag (gtag.js) - Google Analytics