Merging other languages

This commit is contained in:
James Miranda
2016-09-23 18:01:10 -03:00
parent 380a8ee74c
commit de3c5bdaa4
490 changed files with 24539 additions and 24588 deletions

View File

@@ -1,52 +1,53 @@
# 2.1 你好,Go
在开始编写应用之前我们先从最基本的程序开始。就像你造房子之前不知道什么是地基一样编写程序也不知道如何开始。因此在本节中我们要学习用最基本的语法让Go程序运行起来。
## 程序
这就像一个传统,在学习大部分语言之前,你先学会如何编写一个可以输出`hello world`的程序。
准备好了吗?Let's Go!
package main
import "fmt"
func main() {
fmt.Printf("Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちはせかい\n")
}
输出如下:
Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちはせかい
## 详解
首先我们要了解一个概念Go程序是通过`package`来组织的
`package <pkgName>`(在我们的例子中是`package main`)这一行告诉我们当前文件属于哪个包,而包名`main`则告诉我们它是一个可独立运行的包,它在编译后会产生可执行文件。除了`main`包之外,其它的包最后都会生成`*.a`文件(也就是包文件)并放置在`$GOPATH/pkg/$GOOS_$GOARCH`以Mac为例就是`$GOPATH/pkg/darwin_amd64`)。
>每一个可独立运行的Go程序必定包含一个`package main`,在这个`main`包中必定包含一个入口函数`main`,而这个函数既没有参数,也没有返回值。
为了打印`Hello, world...`,我们调用了一个函数`Printf`,这个函数来自于`fmt`包,所以我们在第三行中导入了系统级别的`fmt`包:`import "fmt"`
包的概念和Python中的package类似它们都有一些特别的好处模块化能够把你的程序分成多个模块)和可重用性(每个模块都能被其它应用程序反复使用)。我们在这里只是先了解一下包的概念,后面我们将会编写自己的包。
在第五行中,我们通过关键字`func`定义了一个`main`函数,函数体被放在`{}`大括号就像我们平时写C、C++或Java时一样。
大家可以看到`main`函数是没有任何的参数的我们接下来就学习如何编写带参数的、返回0个或多个值的函数。
第六行,我们调用了`fmt`包里面定义的函数`Printf`。大家可以看到,这个函数是通过`<pkgName>.<funcName>`的方式调用的这一点和Python十分相似。
>前面提到过,包名和包所在的文件夹名可以是不同的,此处的`<pkgName>`即为通过`package <pkgName>`声明的包名,而非文件夹名。
最后大家可以看到我们输出的内容里面包含了很多非ASCII码字符。实际上Go是天生支持UTF-8的任何字符都可以直接输出你甚至可以用UTF-8中的任何字符作为标识符。
## 结论
Go使用`package`和Python的模块类似来组织代码。`main.main()`函数(这个函数位于主包是每一个独立的可运行程序的入口点。Go使用UTF-8字符串和标识符(因为UTF-8的发明者也就是Go的发明者之一),所以它天生支持多语言。
## links
* [目录](<preface.md>)
* 上一节: [Go语言基础](<02.0.md>)
* 下一节: [Go基础](<02.2.md>)
# 2.1 Hello, Go
Before we start building an application in Go, we need to learn how to write a simple program. You can't expect to build a building without first knowing how to build its foundation. Therefore, we are going to learn the basic syntax to run some simple programs in this section.
## Program
According to international practice, before you learn how to program in some languages, you will want to know how to write a program to print "Hello world".
Are you ready? Let's Go!
package main
import "fmt"
func main() {
fmt.Printf("Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界\n")
}
It prints following information.
Hello, world or 你好,世界 or καλημ ́ρα κóσμ or こんにちは世界
## Explanation
One thing that you should know in the first is that Go programs are composed by `package`.
`package<pkgName>` (In this case is `package main`) tells us this source file belongs to `main` package, and the keyword `main` tells us this package will be compiled to a program instead of package files whose extensions are `.a`.
Every executable program has one and only one `main` package, and you need an entry function called `main` without any arguments or return values in the `main` package.
In order to print `Hello, world…`, we called a function called `Printf`. This function is coming from `fmt` package, so we import this package in the third line of source code, which is `import "fmt"`
The way to think about packages in Go is similar to Python, and there are some advantages: Modularity (break up your program into many modules) and reusability (every module can be reused in many programs). We just talked about concepts regarding packages, and we will make our own packages later.
On the fifth line, we use the keyword `func` to define the `main` function. The body of the function is inside of `{}`, just like C, C++ and Java.
As you can see, there are no arguments. We will learn how to write functions with arguments in just a second, and you can also have functions that have no return value or have several return values.
On the sixth line, we called the function `Printf` which is from the package `fmt`. This was called by the syntax `<pkgName>.<funcName>`, which is very like Python-style.
As we mentioned in chapter 1, the package's name and the name of the folder that contains that package can be different. Here the `<pkgName>` comes from the name in `package <pkgName>`, not the folder's name.
You may notice that the example above contains many non-ASCII characters. The purpose of showing this is to tell you that Go supports UTF-8 by default. You can use any UTF-8 character in your programs.
## Conclusion
Go uses `package` (like modules in Python) to organize programs. The function `main.main()` (this function must be in the `main` package) is the entry point of any program. Go supports UTF-8 characters because one of the creators of Go is a creator of UTF-8, so Go has supported multiple languages from the time it was born.
## Links
- [Directory](preface.md)
- Previous section: [Go basic knowledge](02.0.md)
- Next section: [Go foundation](02.2.md)