Learn GO programming essentials summary

  • 2020-05-30 20:23:57
  • OfStack

If you want to start learning GO grammar, please memorize the following four points:

1. Environmental 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 directory

CGO_ENABLED: whether CGO is enabled

CC

CXX

GOGCCFLAGS

GORACE: data synchronous detection, with go test, go run, go build, go install -race options.

GOPATH: GO package lookup path

2. Code organization:

- $GOPATH contains multiple workspace

-workspace contains src, pkg, bin

-src contains package, program, library

-package contains go file

To understand:

* package name vs package path

package name means identifie.package path means the path relative to GOPATH using the package declaration in go file; same package path, same package name.

package import full syntax:

import [name] "path"

Among them:

The name part will do

No, use the default name declared by package
Click to use the current package
blank, init only
name, custom name
The path part will do

Absolute path: the path relative to $GOPATH
Relative path: the path relative to the current file.

Note: relative paths cannot occur using the vendor mechanism, otherwise the parsing will go wrong.

* 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.

test file has the suffix _test.
test func is prefixed with Test.

4. Remote package

Get the remote package using the go get command.

But the go test command relies on git or svn.


Related articles: