quapona technologies
The Go Gopher

In the second part of our Go series, we’ve learned how to install Go, setup the environment and compile our first Go program. In this part we will get to know Go's build-in tools. A full overview of all available Go commands can be found here.

While installing Go not only build tools will be installed, but also several other tools, which ease the process of development, deployment and code management. You’ve already used three of the Go tools while following the last article (go env, go build and go install).

This article is split in three chapters, which will be released one by one in the coming days. The first one will give you an overview of the standard commands for everyday use. The main tools for programming is discussed in the second section. In the third chapter, we present the more advanced tools and their use cases.

Please note, that we won’t talk about all the available arguments for every single tool. Instead, we will show the most useful ones in later examples of this series. As always, if you feel there is something missing or you have a question to a specific command, feel free to leave us a message.

If you haven’t read the first two parts before, please follow this link and this link.

Have Smart Friends

Now, that we have covert the tools to provide more comfort in coding, we will get to know the really smart tools, which makes Go a good out-of-the-box language.

go get

If you ever finished a big C/C++ project, you probably used some (third-party) libraries. Commonly, you will clone them from Github, save the precompiled binaries somewhere in-house or maintain a copy by yourself.  After the years, we looked enviously on all the Java, Python, Ruby, Objective-C and web programmers with their package managers. But now, we are glad to call a package manager our own in form of the go get command.

To get right into it, we download the official hello world program from the official golang Github repository.

$ go get -x -v github.com/golang/example/hello 
github.com/golang/example (download)
cd .
git clone https://github.com/golang/example /home/user/golang/src/github.com/golang/example
cd /home/user/golang/src/github.com/golang/example
git show-ref
cd /home/user/golang/src/github.com/golang/example
git checkout master 
WORK=/tmp/go-build641622168 
github.com/golang/example/stringutil
mkdir -p $WORK/github.com/golang/example/stringutil/_obj/
mkdir -p $WORK/github.com/golang/example/
cd /home/user/golang/src/github.com/golang/example/stringutil
/usr/lib/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/golang/example/stringutil.a -trimpath $WORK -p github.com/golang/example/stringutil -complete -buildid bade8f6965946efe6b0475e8ee15bce0c4331107 -D _/home/user/golang/src/github.com/golang/example/stringutil -I $WORK -pack ./reverse.go
mkdir -p /home/user/golang/pkg/linux_amd64/github.com/golang/example/
mv $WORK/github.com/golang/example/stringutil.a /home/user/golang/pkg/linux_amd64/github.com/golang/example/stringutil.a 
github.com/golang/example/hello
mkdir -p $WORK/github.com/golang/example/hello/_obj/
mkdir -p $WORK/github.com/golang/example/hello/_obj/exe/
cd /home/user/golang/src/github.com/golang/example/hello
/usr/lib/go/pkg/tool/linux_amd64/compile -o $WORK/github.com/golang/example/hello.a -trimpath $WORK -p main -complete -buildid 44dbc9ae10dec215bfc185b7423080014af2c11f -D _/home/user/golang/src/github.com/golang/example/hello -I $WORK -I /home/user/golang/pkg/linux_amd64 -pack ./hello.go
cd .
/usr/lib/go/pkg/tool/linux_amd64/link -o $WORK/github.com/golang/example/hello/_obj/exe/a.out -L $WORK -L /home/user/golang/pkg/linux_amd64 -extld=gcc -buildmode=exe -buildid=44dbc9ae10dec215bfc185b7423080014af2c11f $WORK/github.com/golang/example/hello.a 
mkdir -p /home/user/golang/bin/
mv $WORK/github.com/golang/example/hello/_obj/exe/a.out /home/user/golang/bin/hello 
:$ hello
Hello, Go examples! 

The go get command prints quite a lot of output in the terminal when using the arguments -v and -x. Lets see what's happening. In the beginning, the go get command is cloning the sources with git. Thereby, go get tries to connect with the protocols https, git+ssh and ssh in combination with the import given path. In the example above, the first try to connect with https succeeds and it downloads the repository golang/example from Github. All the following steps should be familiar to you, as the go get tool builds and installs the package inside the hello folder for you. During the build steps of the imported package stringutil there is an addition to the build command, which moves the created archive at the end of the compile command to a folder at $GOPATH/pkg. This newly folder is used to search for packages to link and, therefore, might save you time instead of compiling every source file over and over again.

go test

What's better than working code? Tested code! Go comes with a good testing environment. All test files can be run with the go test command, which looks for .go-files with _test as file name suffix.
As we will discuss the whole test topic in an extra series, we won't spoil anything here. Follow us to get notified about the upcoming "Testing with Go" series.

go generate

The go generate command is one of the newer tools in the Go tool family. It is intended to be used by the software creators, rather than the programmers, who will use your package. In many projects, we will need to use external tools to create either extra code or to compile libraries from other languages with another tool. The easiest approach we use currently is the classical shell script. Here, the go generate command comes into play.  Here is a brief example .go-file:

//go:generate echo "Generating 100% Awesome Content"
//go:generate go run main.go
package main
import "fmt"
func main() {
    fmt.Println("You are an awesome and smart programmer!");
}
 

The first and second line in the new main.go file are those which are processed by go generate. It is important that you write the tag "//go:generate" without spaces or indentation. Everything following after the tag, will be interpreted as exec arg0 arg1 arg2 … argn. Lets try to process or generate commands in the terminal.

:$ go generate
Generating 100% Awesome Content
You are an awesome and smart programmer!
 

As you see, the go generate tool in this example isn't spectacular at all. But we will use it in our series to generate code for e.g. remote procedure calls, to build external libraries on the fly and to automate all the stuff to setup projects to continue work on a project right from the start. So stay tuned on this topic as well.

Now, we should be prepared to start programming with Go in everyday business. This includes to build flexible, maintainable projects, which can be pulled from a remote repository with go get. We can write programs with ease using go fmt, go vet and go doc. Moreover, we can create, install and run executables of our projects with go build, go run and go install.

In the next articles you will find more in depth information about IDEs, testing, debugging and many many more.
We hope you enjoyed this article.
Feel free to leave feedback in the comments area below.

 

The quapona® technologies Development Team

Kommentare powered by CComment

Beitrag teilen