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 part before, please follow this link.

Have Best Friends

After looking at the everyday tools, this section introduces those who will assist you writing clean Go source code. 

go fix

From time to time it is necessary to update the Go installation on your machine to get the latest fixes or to meet requirements of third-party libraries. To cut a long story short, the go fix command updates your Go programs' APIs . So, we recommend to execute the command every time the Go packages have been updated through the apt-get upgrade command or at least if you check out a project from your version control system.

go clean

Keeping your workspace clean and shiny can be exhausting. If you are using git as version control, you might use the git clean command to remove unversioned runtime generated data from tests or other programs. Go has a similar tool called go clean. Look at the listing below, to see how it works.


:$ go build 
:$ go clean -x 
cd /home/user/golang/src/my.domain.com/awesomeuser/firstprog 
rm -f firstprog firstprog.exe firstprog.test firstprog.test.exe main main.exe 

With the -x argument the go clean command is printing all the commands it is using during execution. As you can see it is trying to delete all those files the go build command might produce on the different platforms Go supports and all test builds generated by the go test command.

go doc

Documenting the code you are writing is one of the best habits a programmer can have. Back in the old days, the only way to see the documentation of a class, method, variable etc. was to look into the header files or, at worst, in the source code, if it was available for you. Today all the IDEs and web sites have documentations online. The go doc tool provides a basic way in searching trough the documentation of packages by just typing it in the terminal. Assume you have the two files

src/my.domain.com/awesomeuser/firstprog/printer/printer.go 
     //This is a printer package to print awesome news. 
    package printer
    import "fmt"
    //Prints the awesome message of the day. 
    func Print() { 
        fmt.Printf(getMessage()) 
    }
    //getMessage is a private function, but still awesome. 
    func getMessage() string { 
        return "You are an awesome programmer!" 
    }
 
src/my.domain.com/awesomeuser/firstprog/main.go 
     //The main package. 
    package main
    import "my.domain.com/awesomeuser/firstprog/printer"
    //The main  entry point. 
    func main() { 
        printer.Print() 
    } 

Now type in the terminal the go doc command from the firstprog folder

:$ go doc 
The main package. 
:$ go doc main 
doc: symbol main not present in package main installed in "my.domain.com/awesomeuser/firstprog"
:$ go doc printer 
package printer // import "my.domain.com/awesomeuser/firstprog/printer"
This is a printer package to print awesome news. 
func Print()
:$ go doc Print 
func Print()
    Prints the awesome message of the day. 


As you see the main package is no package you can explicitly document. Which makes sense, because the main file should be as minimal as possible anyway. But with the special argument -u the go doc is printing all the symbols in the given packages.

:$ go doc -u main
func main()
    The main entry point.
 

This will print the documentation of the main function in the main package. Printing the documentation of the package printer with -u results in

:$ go doc getMessage
doc: symbol getMessage not present in package printer installed in "my.domain.com/awesomeuser/firstprog/printer"
:$ go doc -u getMessage 
func getMessage() string
    getMessage is a private function, but still awesome. 

As you see, this is a more or less comfortable way to explore the documentation of packages. Later in the series, we present a way to export the documentation as a website to easily browse documentation and code.

go fmt

Google equipped the Go language with a very handy tool to format your program to a standard format. Take, for example, the following Go program code as our new main.go content

package main
import "fmt"
func printText(){ 
    fmt.Println( "You are an awesome programmer!" ); 
} 
func main(){ 
    printText(); 
} 

This code is really a mess and hard to read due to the bad format. But thankfully, the developers of Go decided to define a set of rules how Go code has to be formatted. The go fmt command checks automatically our source code against these rules of the Go language and rewrites our code as a well-formed Go program.

:$ go fmt -x main.go  
     /usr/lib/go/bin/gofmt -l -w main.go 
     main.go 

Again, the -x argument is printing the commands used during execution. Here the go fmt command is calling the gofmt tool with the parameter -l and -w, which just lists the formatted file and suppresses the output to the terminal and instead writes the output back to the original file. Now, the well-formed code is

package main
import "fmt"
func printText() { 
    fmt.Println("You are an awesome programmer!") 
}
func main() {
    printText()
} 

go vet

After the great automatic formatting function, the next tool is all about static analysing your code. Executing the go vet tool on either files, directories or packages, you will be notified of any location with possibly erroneous code. For example, vetting the next listing

package main
import "fmt"
func main() {
    counter := 0
    fmt.Printf("You are an awesome %v!")
} 

creates the output

:$ go vet main.go  
main.go:7: missing argument for Printf("%v"): format reads arg 1, have only 0 args 
exit status 1 

that signalizes a missing input parameter for the formatted print function Printf().To discuss the very exhausting list of parameter of the go vet command, is out of scope for this article. Thankfully, all the available parameters are used by default. If you want to see the full list of parameters for yourself, you can use go doc cmd/vet to get the full list.

Note: The go vet tool gives only hints on possible error. The decision, if those warnings are right or wrong is the choice of the programmer.

The tools in this article are mentioned to support your work while creating Go programs. We will see them again in the article about the IDE of our choice, that utilizes them in built-in plugins. 

We hope you’ve enjoyed this part, so stay  tuned for the last one, where we discuss the advanced tools and give a hint how we’ll use them in the near future.

Thank you for your time.
The quapona® technologies Developer Team

Comments powered by CComment