If you want to start learning go grammar, please memorize the following 4 points first:
1. Environment variables:
Use Go env to view environment variables
Goarch/gohostarch: Architecture, AMD64 or 386
Goos/gohostos: Operating system, Linux or Windows
Goroot:go installation directory
Gobin:go Program Directory
Gotooldir:go Tools Catalog
Cgo_enabled: Whether to enable CGO
Cc
CXX
Gogccflags
Gorace: Data sync detection, with Go test, go run, go build, go install-race option.
Gopath:go Package Lookup Path
2. Code organization:
-$GOPATH contains multiple workspace
-Workspace contains src, pkg, bin
-SRC contains the package, which is program, library
-Package contains go file
Need to understand:
* Package name vs Package path
Package name refers to the Identifie in go file that uses the package declaration. The package path refers to a relative gopath path. Same package path, same package name.
Package Import Complete Syntax:
Import [Name] "path"
which
The name section can
- None, use the default name of the package declaration
- Points, using the current package
- Blank, execute init only
- Name, custom name
The path section can
- Absolute path: A relative $gopath path
- Relative path: The path relative to the current file.
Note: You cannot use the vendor mechanism to have a relative path, otherwise the parsing error occurs.
* Program vs Library
Package name for main means program
Package name non-main means library
3. Code Testing
Execute the test code using the Go Test command.
- The test file is suffixed with _test.
- Test func is prefixed with test.
4. Remote Package
Use the go get command to get the remote package.
But the Go Test command relies on git or svn.
How do I start go programming?