Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Six Reasons Why I Love Go

DZone's Guide to

Six Reasons Why I Love Go

With a quick development process, no dependencies, and standardized formatting for easy code review, working with Go is awesome. Check out all six of my reasons for going with Go here.

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

The past 15 years of my career most of my work has been in Java, C++, or Objective-C for IOS applications.  This past year I have been working a lot in Go and have had many of my Java peers ask me what makes Go so great to work with. Here’s 6 high level reasons why I love Go.

1. Quick Development Process

Go is an extremely easy language to learn.  Go is a C-like language, both in it looks like C and allows you to reference C libraries.  If your looking into learning Go, I suggest going through A Tour of Go which can be done within a few hours.  Another good reference is reading Pointers in Go

Go is extremely quick at compilation.  You can compile a 5mb, statically typed binary in a second or less. This stemmed from what Rob PikeKen Thompson, and Robert Griesemer started theorizing about Go while waiting for a large C++ to build.  This started on a whiteboard in 2007 and shortly thereafter turned into a specification.

Example: Hello World

package main

import (
     "fmt"
)

func main() {
      fmt.Println("Hello World")
}

Go has an extensive standard library that offers developers a lot of flexibility.  One feature that stood out to me when I was initially learning Go was the ability to quickly build a web server in Go.

Lets redo our Hello World example above so you can access it via a web browser.

Example 2: Hello World through HTTP

  • Create a file named helloweb.go and copy and paste the following code:
  • package main
    
    import (
        "io"
        "net/http"
    )
    
    func hello(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "Hello world!")
    }
    
    func main() {
        http.HandleFunc("/", hello)
        http.ListenAndServe(":8000", nil)
    }
    
    1. In terminal, execute the command go run helloweb.go.
    2. Open your browser to http://localhost:8000 and you will see Hello world! appear.

    Wasn’t that easy? The example above listens on port 8000 and directs requests to the hello function which outputs “Hello world!”

    2. Hasta la Vista Dependencies

    When you compile in Go the end result is a statically linked binary that runs natively on the target platform.  This means you no longer need to install Java (VM & Dependencies),  Python, Ruby + Gems, just to run your program.

    Go supports seamless cross compilation for Windows, OS X, Linux, and BSD.  I’m a Mac user and found it very cool that I could cross compile a Windows binary for my windows based engineers.

    Here’s a few examples of compiling a Windows and Linux binary on a Mac.

    Windows:

    GOOS=windows GOARCH=386 go build -o appname.exe

    Linux:

    GOOS=linux GOARCH=amd64 go build -o appname.linux

    3. One Format Fits All

    Code reviews are a best practice that every quality team should be doing.  When reviewing code you're also having to spend time to insure the formatting is meeting the teams guidelines.   This wastes time when really we should be spending time on the code and mentoring.

    GoFmt solves this by formatting your source code into the standard format. Time is saved and arguments of tabs vs. spaces are history. There is one universal format and this is GoFmt.

    4. Concurrency Not Parallelism

    Go was designed with concurrency first.  Go’s approach to concurrency originates in Hoare’s Communicating Sequential Processes (CSP).

    The keyword go starts a go-routine which is a lightweight thread essentially. When you execute a go-routine there is no need to worry about which processor it runs on. Shared values between routines are passed around on channels. You epoll those channels using the select keyword. Only one goroutine has access to the value at any given time which eliminates race conditions.

    I could write a whole post about go-routines but instead you should watch this great video by Rob Pike, entitled Concurrency is not Parallelism.

    Example: Go-Routine Fibonacci sequence

    package main
    
    import "fmt"
    
    func fibonacci() chan int {
      c := make(chan int)
    
      go func() { 
        for i, j := 0, 1; ; i, j = i+j,i {
            c <- i
        }
      }()
    
      return c
    }
    
    func main() {
        c := fibonacci()
        for n := 0; n < 12 ; n++ {
            fmt.Printf("%d ", <- c)
        }
    }

    The example above starts a go-routine and returns a channel to the fibonacci sequence.  Every iteration reads a number from the channel c.   Try the above example out in the Go Playground.

    5. Built in Testing

    In Go, testing is built into the language.  Name a file with the suffix of _test.go and it’ll only build within the test lifecycle.  You run tests by executing go test in the directory.  Go also allows you to have built-in benchmarks that are controlled by the go tool allowing your test to run enough iterations to get an impactful result, displayed in operations per second.

    6. Go Docs

    HTML (aka Go Doc) documentation is built into Go.  You no longer need to worry about annotated comments or other directives to produce nicely formatted docs.  Plaintext comments are turned into very simplistic, modern based documentation.

    Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

    Topics:
    golang ,webserver ,web dev

    Published at DZone with permission of Jeremy Unruh, DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    THE DZONE NEWSLETTER

    Dev Resources & Solutions Straight to Your Inbox

    Thanks for subscribing!

    Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

    X

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}