-
Notifications
You must be signed in to change notification settings - Fork 12
Go Module Development
Zhaobo edited this page Sep 21, 2022
·
5 revisions
curl -LO https://go.dev/dl/go1.18.6.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.18.6.linux-amd64.tar.gz
Add the following Go environment to .bashrc
and source .bashrc
to apply
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
E.g., module name alnair-profiler, mkdir alnair-profiler
Create a go module reference.
cd alnair-profiler
go mod init alnair-profiler
Create sub-folder pkg for package source code, which can be sorted into multiple packages
Import external package, use go mod tidy
automatically download the package and update the go.mod and go.sum file
Create sub-folder cmd to call the package as the main entry, reference
In this way, package and cmd can be easily extended.
alnair-profiler/
---pkg/
---profiler/
server.go
---cmd/
---profiler/
main.go
---go.mod
---go.sum
server.go example
package profiler
import log
func SayHi(){
log.Println("Hi")
}
main.go example
package main
import (
"alnair-profiler/pkg/profiler"
)
func main() {
profiler.SayHi()
}
To run the program go run cmd/profiler/main.go
. Complete example module go can be found here
Install Go Nightly extension to enable rich language support, e.g., auto list methods of structs, check unused library, etc.
- Connect to the cluster 1: in-cluster client configuration
- Connect to the cluster 2: out-of-cluster client configuration, download the kubeconfig file and provide absolute path to the file.
- Callable functions outside the package starts with Capital letters. Functions started with small letters are not callable outside package.