Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go Systems Programming

You're reading from   Go Systems Programming Master Linux and Unix system level programming with Go

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher Packt
ISBN-13 9781787125643
Length 466 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mihalis Tsoukalos Mihalis Tsoukalos
Author Profile Icon Mihalis Tsoukalos
Mihalis Tsoukalos
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started with Go and Unix Systems Programming 2. Writing Programs in Go FREE CHAPTER 3. Advanced Go Features 4. Go Packages, Algorithms, and Data Structures 5. Files and Directories 6. File Input and Output 7. Working with System Files 8. Processes and Signals 9. Goroutines - Basic Features 10. Goroutines - Advanced Features 11. Writing Web Applications in Go 12. Network Programming

Two useful Go tools

The Go distribution comes with a plethora of tools that can make your life as a programmer easier. The two most useful of them are gofmt and godoc.

Note that go tool itself can also invoke various tools: you can see a list of them by executing go tool.

The gofmt utility formats Go programs in a given way, which is really important when different people are going to work with the same code for a big project. You can find more information about gofmt at https://golang.org/cmd/gofmt/.

The following is a poorly formatted version of the hw.go program that is hard to read and understand:

$ cat unglyHW.go
package main
    
import
    "fmt"
    
// This is a demonstrative comment!
        func main() {
  fmt.Println("Hello World!")
    
}

Processing the previous code, which is saved as unglyHW.go with gofmt, generates the following easy to read and comprehend output:

$ gofmt unglyHW.go
package main
    
import "fmt"
    
// This is a demonstrative comment!
func main() {
      fmt.Println("Hello World!")
    
}

Remembering that the gofmt utility does not automatically save the generated output is important, which means that you should either use the -w option followed by a valid filename or redirect the output of gofmt to a new file.

The godoc utility allows you to see the documentation of existing Go packages and functions. You can find more information about godoc at http://godoc.org/golang.org/x/tools/cmd/godoc.

You are going to use godoc a lot as it is a great tool for learning the details of Go functions.

The following screenshot shows the output of the godoc command generated on a Terminal when asked for information about the Println() function of the fmt package:

The output of the godoc command

Another handy feature of godoc is that it can start its own web server and allow you to see its documentation using a web browser:

$ godoc -http=:8080  

The following screenshot shows the kind of output you get on a web browser after visiting http://localhost:8080/pkg/ while the previous command is running. You can use any port number you want, provided that it is not already in use:

Using the godoc utility from your web browser

The most important tool for a programmer is the editor they use for writing the source code. When I am on a Mac, I typically use the TextMate editor, but when I am on a different Unix machine, I prefer vi. Choosing an editor is not an easy task because you are going to spend a lot of time with it. However, any text editor will do the job as long as it does not put any control characters inside the source code files. The following screenshot shows the TextMate editor in action:

The TextMate editor showing the look of a some Go code

Advantages and disadvantages of Go

Go is not perfect but it has some very interesting features. The list of the Go strong features includes the following:

  • Go code is easy to read and easy to understand.
  • Go wants happy developers because a happy developer writes better code!
  • The Go compiler prints practical warning and error messages that help you solve the actual problem. Putting it simply, the Go compiler is there to help you, not to make your life difficult!
  • Go code is portable.
  • Go is a modern programming language.
  • Go has support for procedural, concurrent, and distributed programming.
  • Go supports Garbage Collection (GC) so you do not have to deal with memory allocation and deallocation. However, GC might slow down your programs a little.
  • Go does not have a preprocessor and does high-speed compilation. Consequently, Go can be used as a scripting language.
  • Go can build web applications. Building a web application in C is simply not very efficient unless you use a nonstandard external library. Additionally, Go provides programmers with a simple web server for testing purposes.
  • The standard Go library offers many packages that simplify the work of the programmer. Additionally, the methods found in the standard Go library are tested and debugged in advance, which means that most of the time they contain no bugs.
  • Go uses static linking by default, which means that the produced binary files can be easily transferred to other machines with the same OS. Consequently, the developer does not need to worry about libraries, dependencies, and different library versions.
  • You will not need a GUI for developing, debugging, and testing Go applications as Go can be used from the command line.
  • Go supports Unicode. This means that you do not need any extra code to print characters from multiple human languages.
  • Go keeps concepts orthogonal because a few orthogonal features work better than many overlapping ones.

The list of Go disadvantages includes the following:

  • Well, Go is not C, which means that you or your team should learn a new programming language to develop systems software.
  • Go does not have direct support for object-oriented programming, which can be a problem for programmers that are used to writing code in an object-oriented manner. Nevertheless, you can use composition in Go to mimic inheritance.
  • Back when Unix was first introduced, C was the only programming language for writing systems software. Nowadays, you can also use Rust, C++, and Swift for writing systems software, which means that not everybody will be using Go.
  • C is still faster than any other programming language for systems programming mainly because Unix is written in C.
Despite the advantages or the disadvantages of a programming language, you have the final word on whether you like it or not. The important thing is that you choose a programming language that you like and can do the job you want! Personally, I do not like C++ despite the fact that it is a very capable programming language and I have written an FTP client in C++! Additionally, I never liked Java. There is no right or wrong thing in personal tastes so do not feel guilty about your choices.
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image