Go Language Introduction
Similar to C and C++, Go is a compiled language. Go source programs are compiled by the Go compiler to generate standalone binary executable files in ELF (Linux) or EXE (Windows) format, which can be run without requiring the Go source code at runtime.
How to Use Third-Party Modules in Go
Go uses the import statement to include third-party modules. Example code:
package main
import (
tingyun "github.com/TingYunGo/goagent"
)
func RoutineID() int64 {
return tingyun.GetGID()
}
Here, tingyun is the alias for the imported module.
If you only want to import a module without directly using its methods, you need to use the underscore _ as the alias, otherwise the Go compiler will report an error:
package main
import (
_ "github.com/TingYunGo/goagent"
)
How to Download and Install Third-Party Modules Locally in Go
All third-party modules in Go are published as source code on Git servers. Download and installation of third-party modules are divided into GOPATH mode and GOMOD mode.
-
GOPATH Mode: For Go versions below 1.11, or when GOMOD is disabled (set the environment variable GO111MODULE=off), GOPATH mode is used. In this mode, the Go compiler checks the GOPATH environment variable and searches for third-party libraries by name in each path specified by GOPATH. If GOPATH is not set, the default is the go folder under your home directory. In GOPATH mode, there are two ways to install third-party modules:
-
Automatic installation: Use the command
go get <third-party-library-path>. For example, to install the Tingyun Go Agent:$ go get github.com/TingYunGo/goagentAfter executing the command, the module will be automatically downloaded to src/github.com/TingYunGo/goagent under GOPATH, and all dependent modules will be recursively downloaded to the corresponding directories.
-
Manual installation: Manually create the directory src/github.com/TingYunGo/goagent under GOPATH, and copy the code to this folder.
-
-
GOMOD Mode: For Go versions 1.11 and above, and when the environment variable GO111MODULE=on, GOMOD mode is used. In GOMOD mode, a go.mod file is required in the root directory of the Go application, specifying the application name, Go version, and dependencies with their versions. Example:
module http_example
go 1.12
require (
github.com/TingYunGo/goagent v0.7.8
github.com/golang/protobuf v1.5.2 // indirect
)Where:
- http_example is the application name.
- go 1.12: requires Go version not lower than v1.12.
- require: specifies the third-party modules and their versions.
To download dependencies in GOMOD mode:
- Use the command
go mod tidy, which will automatically check the current application's dependencies, download all required packages, and verify their hash values, writing them to the go.sum file.
How to Compile a Go Application
Navigate to the application source code path and run the go build command to generate the executable file for the application.