This article mainly includes:
- Go installation and configuration
- Atom as IDE
- Go VS Python
I have been learning Go for a while, using Vim as the IDE, and trying to use Go as the server backend several times. Later, I left it for a while because I didn't need to write the website again. Now recall the experience with Go, which may have the following advantages over Python:
- High performance, high performance and high performance;
- Low memory, low memory, low memory;
The disadvantages are:
- The library is too small (compared to Python);
- Built-in data structure and operation is too simple;
- Third-party library dependence, installation, management trouble
The development of Go is still quite good, at least in terms of server and backend (high-energy, low-power binary package) is still very attractive. The Go language environment has not been configured on the new computer. It is just reinstalled and configured this time, and compared with Python from the basic syntax, so that interested Python developers can quickly try the Go language and take notes.
Go installation and configuration
All installation and configuration steps are clearly stated on the official website. Mac OS X has a pkg version, which can be directly double-clicked. The Go language has several very important environment variables that need to be clarified and set:
The installation directory of Go. This directory is where Go looks for the built-in library. If the default location of the Mac OS X installation package is /usr/local/go, if it is manually installed from the source, if it is not installed to /usr/local/go, set it to Installation directory (if the default location does not need to be set):
Your working directory. This directory is used to store third-party libraries downloaded from the Internet as well as projects you create yourself. Unlike Python, Go's goal is not to create scripts that can be executed anywhere, but in terms of projects. You can choose a directory as your Go working directory:
In addition to these two necessary environment variables, you can also set the system information:
The last GOBIN is the directory where the generated binary commands are saved when go get some-package. If you do not set the default to $GOPATH/bin, it is best to add this directory to the system path (if set to /usr/local/go/bin also needs to pay attention to permission issues):
Now even if the installation configuration is complete, execute:
Try installing a command line tool again:
Go get -u github.com/astaxie/bat
The bat tool will be installed under $GOBIN, when we return to the working directory, we will find that the directory structure becomes:
Cd $GOPATH && tree -L 1
Among them, src stores various third-party libraries and source files of their own projects, bin is a binary executable file generated by go get or go install, and pkg is a platform-related package. Next, configure Atom as a Go-specific IDE!
- Atom as Go IDE
I have configured Vim before, but most of the time I use Atom, and Atom's plugin management is more convenient, so I use Atom as the IDE!
The plugin to be installed first is go-plus, but it turns out that it has a bunch of dependent plugins that need to be installed, and these plugins rely on some Go tools, but now Atom can automatically install dependent plugins, no need to manually install one:
These plugins may need some Go tool support, such as golint, goimport, gocode, etc. Here is one of the shortcomings mentioned above. If it is a library hosted on github, it is better, but if golang .org or code.google.com needs to be online, and it has been so badly affected by these third-party libraries that the experience is very poor.
After a long wait, the go-plus dependencies are installed and the Go tool is installed:
Go get -u github.com/nsf/gocode
Go get -u github.com/golang/lint
Cd $GOPATH && mkdir -p golang.org/x
Git clone https://github.com/golang/tools.git
Go install golang.org/x/tools/cmd/goimports
Don't worry about creating a Go file with Atom first, let's take a look at the working directory at this time:
Cd $GOPATHtree -L 2
Go's third-party inventory directory starts with a hosted website (as well as when importing), and projects created by yourself should follow this specification! (Note that there are no executables in my bin directory because I moved them to the system path).
Next create a new project:
Cd $GOPATHmkdir src/github.com/rainyear/learn
If you still think that Python's indentation is obsessive-compulsive, then the real obsessive-compulsive disorder is coming! Go provides the gofmt and goimports tools to help you force the code to be cleaned up, including indentation, curly braces, and import. Just save it in Atom and the code will be rearranged. This is the gospel for OCD patients! However, Google also promoted this benefit to Python and introduced the Python formatting tool: yapf.
In the above image, go run main.go is run through Atom's Builder plugin. If the whole project is completed, you can package the generated binary files with go install (you can even generate cross-platform binary packages and deploy them directly to the line!):
Cd $GOPATHls bin
- Go VS Python
Finally, let's compare some of the basic syntax of Go and Python. The Go example is from A Tour of Go, source file https://github.com/rainyear/learn.go: