This is a creation in Article, where the information may have evolved or changed.
These two days with Go wrote the first project, still in the demo phase, follow-up will continue to record some of the pit points about go development.
The project, called Bigpipe, is designed to solve asynchronous HTTP calls between services, which is very simple to implement with go, and is also good for concurrency.
I made a simple sharing PPT for the project to download.
Project Address: Https://github.com/owenliang/bigpipe, editor must use Gogland, error hints too strong, it is difficult to write the wrong code!
Now there are a few notes:
- Configuration file I use JSON, because go is strongly typed, so parsing json is very painful, but open source Library I do not trust, so use the standard library JSON slowly.
- A go file in a directory belongs to a package, the package is the name of the directory, the package calls each other does not need import.
- The pipe is referenced and does not require pointers.
- When you format a string, the string* and string are different, and the former prints the pointer, which is the value.
- Log library I wrote it myself, with channel transmission to ensure concurrency security, single goroutine sequential write files.
- The context library is a relatively unpopular but must be used for something, you can take a special look.
- The HTTP library is very powerful, both for the server and the client, both asynchronous and concurrent.
- Go comes with Go get package management is too difficult to use, does not automatically download dependencies, there is no version of the concept, you must use third-party package management tools.
- Goroot points to the Go installation directory, Gopath points to the project directory (it looks for the code in the SRC directory), both of which are used to search for code in the import.
- Where to put the program's entrance, but be sure to define it as the package main.
- Analysis Go program performance can be used pprof, it can directly draw the call graph and view in the browser.
- The Go program needs to specify the number of threads according to the runtime package after booting, otherwise all CPU cores may not be used.
- Using interface abstract interfaces to rely on abstractions rather than relying on implementations is consistent with the abstraction of C/c++/java, but the form is too special to be used.
Follow-up will continue to improve the project, casually record some go experience.