Merge commit 'cdaf20eb7fa1cebfcb9cd671e0225ad621e548a1' into ja

This commit is contained in:
Shin Kojima
2014-09-21 22:36:43 +09:00
2 changed files with 14 additions and 14 deletions

View File

@@ -2,17 +2,17 @@
## Go commands
Go language comes with a complete set of command operation tool, you can execute the command line `go` to see them:
The Go language comes with a complete set of command operation tools, you can execute the command line `go` to see them:
![](images/1.3.go.png?raw=true)
Figure 1.3 Go command displays detailed information
These are all useful for us, let's see how to use some of them.
These are all useful for us. Let's see how to use some of them.
## go build
This command is for compiling tests, it will compile dependence packages if it's necessary.
This command is for compiling tests. It will compile dependence packages if it's necessary.
- If the package is not the `main` package such as `mymath` in section 1.2, nothing will be generated after you executed `go build`. If you need package file `.a` in `$GOPATH/pkg`, use `go install` instead.
- If the package is the `main` package, it will generate an executable file in the same folder. If you want the file to be generated in `$GOPATH/bin`, use `go install` or `go build -o ${PATH_HERE}/a.exe.`
@@ -30,7 +30,7 @@ This command is for compiling tests, it will compile dependence packages if it's
## go clean
This command is for clean files that are generated by compilers, including following files.
This command is for cleaning files that are generated by compilers, including the following files.
_obj/ // old directory of object, left by Makefiles
_test/ // old directory of test, left by Makefiles
@@ -43,17 +43,17 @@ This command is for clean files that are generated by compilers, including follo
DIR.test(.exe) // generated by go test -c
MAINFILE(.exe) // generated by go build MAINFILE.go
I usually use this command to clean my files before I upload my project to the Github, these are useful for local tests, but useless for version control.
I usually use this command to clean my files before I upload my project to the Github. These are useful for local tests, but useless for version control.
## go fmt
The people who are working with C/C++ should know that people are always arguing about code style between K&R-style and ANSI-style, which one is better. However in Go, there is only one code style which is forced to use. For example, you must put left brace in the end of the line, and can't put it in a single line, otherwise you will get compile errors! Fortunately, you don't have to remember these rules, `go fmt` does this job for you, just execute command `go fmt <File name>.go` in terminal. I don't use this command very much because IDEs usually execute this command automatically when you save source files, I will talk about IDEs more in next section.
The people who are working with C/C++ should know that people are always arguing about code style between K&R-style and ANSI-style, which one is better. However in Go, there is only one code style which is enforced. For example, you must put a left brace in the end of the line, and can't put it in a single line, otherwise you will get compile errors! Fortunately, you don't have to remember these rules. `go fmt` does this job for you. Just execute the command `go fmt <File name>.go` in terminal. I don't use this command very much because IDEs usually execute this command automatically when you save source files. I will talk about IDEs more in next section.
We usually use `gofmt -w` instead of `go fmt`, the latter will not rewrite your source files after formatted code. `gofmt -w src` formats the whole project.
## go get
This command is for getting remote packages, it supports BitBucket, Github, Google Code, Launchpad so far. There are actually two things happening after we executed this command. The first thing is to download source code, then executes `go install`. Before you use this command, make sure you have installed related tools.
This command is for getting remote packages, it supports BitBucket, Github, Google Code, Launchpad so far. There are actually two things that happen after we executed this command. The first thing is to download the source code, then execute `go install`. Before you use this command, make sure you have installed all the related tools.
BitBucket (Mercurial Git)
Github (git)
@@ -64,24 +64,24 @@ In order to use this command, you have to install these tools correctly. Don't f
## go install
This command compiles all packages and generate files, then move them to `$GOPATH/pkg` or `$GOPATH/bin`.
This command compiles all packages and generates files, then moves them to `$GOPATH/pkg` or `$GOPATH/bin`.
## go test
This command loads all files whose name include `*_test.go` and generate test files, then prints information looks like follows.
This command loads all files whose name include `*_test.go` and generates test files, then prints information that looks like the following.
ok archive/tar 0.011s
FAIL archive/zip 0.022s
ok compress/gzip 0.033s
...
It tests all your test files as default, use command `go help testflag` for more details.
It tests all your test files by default, use command `go help testflag` for more details.
## go doc
Many people said that we don't need any third-party documentation for programming in Go(actually I've made a [CHM](https://github.com/astaxie/godoc) already), Go has a powerful tool to manage documentation by itself.
Many people said that we don't need any third-party documentation for programming in Go (actually I've made a [CHM](https://github.com/astaxie/godoc) already), Go has a powerful tool to manage documentation by itself.
So how to look up packages' information in documentation? If you want to get more details about package `builtin`, use command `go doc builtin`, and use command `go doc net/http` for package `http`. If you want to see more details about specific functions, use command `godoc fmt Printf`, and `godoc -src fmt Printf` to view source code.
So how do we look up package information in documentation? If you want to get more details about the package `builtin`, use command `go doc builtin`, and use command `go doc net/http` for package `http`. If you want to see more details about specific functions, use command `godoc fmt Printf`, and `godoc -src fmt Printf` to view source code.
Execute command `godoc -http=:8080`, then open `127.0.0.1:8080` in your browsers, you should see a localized golang.org. It can not only show the standard packages' information, but also packages in your `$GOPATH/pkg`. It's great for people who are suffering from the Great Firewall of China.

View File

@@ -1,9 +1,9 @@
# 1.5 Summary
In this chapter, we talked about how to install Go through three ways, including from source code, standard package and third-party tools. Then we showed you how to configure Go development environment, mainly about `$GOPATH`. After that, we introduced the steps in compile and deployment of Go programs. Then we talked about Go commands, these commands including compile, install, format, test. Finally, there are many powerful tools to develop Go programs, such as LiteIDE, Sublime Text, Vim, Emacs, Eclipse, IntelliJ IDEA, etc. You can choose any one you like exploring the world of Go.
In this chapter, we talked about how to install Go through using three different methods: including from source code, standard package and via third-party tools. Then we showed you how to configure the Go development environment, mainly covering how to setup your `$GOPATH`. After that, we introduced the steps in compilation and deployment of Go programs. We then covered Go commands, including the compile, install, format and test commands. Finally, there are many powerful tools to develop Go programs such as LiteIDE, Sublime Text, Vim, Emacs, Eclipse, IntelliJ IDEA, etc. You can choose any one you like exploring the world of Go.
## Links
- [Directory](preface.md)
- Previous section: [Go development tools](01.4.md)
- Next chapter: [Go basic knowledge](02.0.md)
- Next chapter: [Go basic knowledge](02.0.md)