Table of Contents
In this article, I will take you through 10 Most Popular GO Command Examples on Linux. You might be wondering why we need to study GO Programming Language. It is because of its wide availability on a variety of platforms, its robust well-documented common library, and its focus on good software engineering principles, Go is a great programming language to learn.
Like other most often used programming languages like Java, GO does not have any provision for try, catch block statements. If someone is looking for a feature like Concurrency, then this is the Programming language you want to learn. In this article, we will go through popularly used go command examples in Linux. We will see more on later articles.
10 Most Popular GO Command Examples in Linux
Example 1: Check GO Command Version
You can check the GO command version by using
go version command as shown below. As you can see from the output, currently installed version is
root@localhost:~# go version go version go1.10.4 linux/amd64
Example 2: Print GO Environment Information
If you want to see all the environment variable information which is used by GO, you can see it by using
go env command as shown below. You might notice from the output, go uses environment variable for setting architecture, PATH, OS, compiler and other important parameters.
root@localhost:~# go env GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOOS="linux" GOPATH="/root/go" GORACE="" GOROOT="/usr/lib/go-1.10" GOTMPDIR="" GOTOOLDIR="/usr/lib/go-1.10/pkg/tool/linux_amd64" GCCGO="gccgo" CC="gcc" CXX="g++" CGO_ENABLED="1"
Example 3: Remove all the Object Files
If you want to remove all the previously created object files from your system, then you need to use
go clean command as shown below. This command will clean up your system.
root@localhost:~# go clean
Example 4: List all Packages and Dependencies
If you want to list all the packages and dependencies, then you need to use
go list command as shown below.
root@localhost:~# go list _/root
Example 5: Build Your Program Using go build
If you want to build your GO Program, you can build it by using
go build command as shown below. This command is usually used when you have large number of GO source code and you want to build those codes.
root@localhost:~/hello# go build
Example 6: Run Your Program Using go run
To build and run your program directly with specifically building the program first, you need to use
go run command as shown below. This command is usually used for small projects where you do not have many code files.
root@localhost:~/hello# go run hello.go hello, world
Example 7: Download Packages Using go get
You can also download and install packages silently using
go get command as shown below. Here I am downloading and installing GO mysql driver from github using
go get command.
root@localhost:~# go get -v github.com/go-sql-driver/mysql github.com/go-sql-driver/mysql (download) created GOPATH=/root/go; see 'go help gopath' github.com/go-sql-driver/mysql
Example 8: Check Current GO Path
If you want to check current GO environment variable PATH, you can use
go env GOPATH command and check that as can be shown below. Similarly you can check other environment variables also using
go env command with variable name as argument.
root@localhost:~# go env GOPATH /root/go
Example 9: Install Packages Using go install
If you want to install your executable from any path, you can use
go install command and install it as shown below. This command is same as
go build command apart from the fact that go build compile and install from the current directory but
go install will push the executable to
root@localhost:~# go install hello
Example 10: Fix Packages Using go fix
Many times you might have observed that updating the go version will do changes in API support and the previous written code might not work properly. In those situations,
go fix command will be very handy to fix those issues and make your code as per the latest release.
root@localhost:~# go fix
Reference: Introducing GO