This is a creation in Article, where the information may have evolved or changed.
The article "How to Write Go code" is golang.org official document, the address is https://golang.org/doc/code.html.
The following is a study note.
The article mainly describes these aspects of the content:
- Code structure & environment variable settings for Go
- HelloWorld Sample Program
- Stringutil Sample Library
- Unit Test
- Extended data (Follow-up learning)
1, Go Code organization structure & environment variable setting
The Go Code organization follows the following rules:
- Generally there is only one workspace
- There are multiple warehouses under a workspace, which are subordinate to different version control tools
- Multiple package per warehouse
- Each package is a stand-alone directory that contains multiple source files
- The catalog of the package determines the import path
Environment variables need to be setGOPATH
2. HelloWorld Sample Program
- If you
go install
do not specify parameters, you need to be in hello.go
your directory;
- If you set parameters, in any directory can be, parameters must be
go install -github.com/user/hello
;
Same below
3. Stringutil Sample Library
go build
Parameters above;
- It is worth noting that the
go build
output is not generated if it is desired stringutil.a
to be executed go install
;
$ go build
This won ' t produce an output file. To doing, you must use go install, which places the package object inside the pkg directory of the workspace.
4. Unit Test
Unit test is not the same as C + +, too wife advanced!
Just write the test function and call the test command on the line.
5, extension of information (follow-up learning) "very important"
See effective go for tips on writing clear, idiomatic go code.
Take A tour of Go to learn the language proper.
Visit the documentation page for a set of in-depth articles about the Go language and its libraries and tools.