DEV Community

Francis Sunday
Francis Sunday

Posted on

How I learned Go Programming

Gopher

Go is a relatively new programming language, and nothing makes a developer go crazier than a new programming language, haha! As many new tech inventions, Go was created as an experiment. The goal of its creators was to come up with a language that would resolve bad practices of others while keeping the good things. It was first released in March 2012. Since then Go has attracted many developers from all fields and disciplines.

During the second quarter of this Year, I joined ViVA XD, an In-flight entertainment platform changing the world one trip at a time. We chose Go to build the whole infrastructure. Go was/is still an awesome choice for us in terms of performance, and its fast and simple development process.

In this article, I'll give a short introduction of the Go language and it would serve as a motivation for further reading.

Getting Started

I had no prior experience with Go, as a norm, Learning a new programming language, I started out by checking the Golang offical webpage. I found the installation instructions for my OS, tutorials, an awesome documentation. It was terrifying (as the first Statically Typed language I've used). The Tour of Go was recommended, its an interactive Go tutorial (comes bundled with Go) which got me familiar with Go's syntax and literals. I finished with the Tour in a couple of days, and got an idea of how things work in the Go environment like variables, types, structs, pointers, lists, etc. At this point, I didn't really grasp all about Go, my experience from other languages gave me enough knowledge during my first task at ViVA XD. Go does an awesome job making us compile our source code to a single binary for any platform. Our app runs on Linux, so if we had a developer working on a Mac she can compile it down easily
GOOS=linux go build and in a matter of seconds a Linux build is ready.

Going deeper

I got familiar with the syntax, and other important part of the Go language, like goroutines, concurrency, error handling, packages, interfaces, two-value assignments, etc.

No Exceptions 😉

In Go, functions usually return a value and an error, but not limited to that alone. Functions can return multiple values. Handling errors in Go is simple, we check the second return value (or the position where the error is returned from the function). This structure gives a good way to chain errors from one point to another.

type Server struct {}

func main() {
    err := s.Run()
}

func (s *Server) Run() (err error) {
    http.HandleFunc("/", s.handleHomeRequest)
    err = http.ListenAndServe(":4000", nil)
    if err != nil {
        return err
    }
    return nil
}
Enter fullscreen mode Exit fullscreen mode

Variable and Declarations

In Go, all variables must be defined. We can't have an assignment like x = 2.

package main

import (
    "fmt"
)

func main() {
    var num int
    num = 20
    fmt.Printf("The number is %d\n", num)
}
Enter fullscreen mode Exit fullscreen mode

We declare a variable num of type int. Go by default, assigns a zero value to variables. Integers are assigned 0, booleans, false, strings "", and so on. That pattern may look like a lot of typing, Go also provides us a handy operator :=.
So we can rewrite the declaration above to num := 29

As long as the variable is new, := can be used.

Two-value Assignment

Interfaces in Go are used to do exactly what they were designed to do in any language, which is to logically group code that has similar behaviour, not to achieve less code.
In Go, every type implements at least zero methods, which means each type implements a special zero interface{}. This is useful when we do no not know what type a variable is.

var someData interface{}
err := json.Unmarshal(raw_bytes, &someData)
if err != nil {
    log.Error(err)
}
Enter fullscreen mode Exit fullscreen mode

To get a type from this, we use Type assertion. It is a simple statement, if the assertion fails, the assigned value will have the default value of the type. Another way to do this is to use a Two-value assignment. We assign a second value in the type assertion statement which will hold a boolean stating if it was successful.

jsonStruct := someData.(map[string]interface{})
num := jsonStruct["num"].(int)
str, ok := jsonStruct["str"].(string)
if !ok {
    log.Error("error converting to string")
}
Enter fullscreen mode Exit fullscreen mode

Same applies for retrieving values from a map

data := make(map[string]int)
data["result"] = 20
val, ok := data["result"] // returns false if key isn't found
if !ok {
    log.info("Value for key not found!!")
}
Enter fullscreen mode Exit fullscreen mode

Goroutines

Most of the modern programming languages like Python, Java, etc originated from the single threaded environment. Most of them supports multi-threading, but they still face some problems that comes with concurrent execution, race conditions, threading-locking and deadlocks. Those of which makes it hard to create a multi-threading application on those languages.

In Java (from research, I don't have experience with Java), creating new threads is not memory efficient. Every thread consumes approx 1MB of memory heap size. If you start spinning thousands of threads, they will put tremendous pressure on the heap and will cause a shut down due to loss of memory. Also, Communication between two or more threads, is very difficult.

Go was released in 2009 when multi-core processors were already available. Go is built with keeping concurrency in mind. Go has goroutines instead of threads. They consume almost 2KB memory from the heap. So one can spin millions of goroutines at any time.

How Goroutines work

Some benefits from using goroutines
  • Goroutines allow you avoid having to resort to mutex locking when sharing data structures.
  • Goroutines have faster startup time than threads.
  • Goroutines makes use of channels, a built-in way used to safely communicate between themselves.
  • Goroutines and OS threads do not have a 1:1 mapping. A single goroutine can run on multiple threads. Goroutines are multiplexed into small number of OS threads.

More on Concurrency by Rob Pike

Written in Go? Its easy to maintain

Go intentionally leaves out many features of modern OOP languages.

  • No classes. Everything is divided into packages. Go has only structs.
  • No support for inheritance. Go makes it easy to understand the code, as there's no super class to look out for.
  • No Execeptions
  • No generics
  • No annotations
  • No constructors

Getting better

After checking all the official Go resources I was constantly on the lookout to get to know more about the Go language, I've come across awesome contents that teaches one Go. There's Go by example, Go Book, Effective Go, among others online.
These resources helped out and is still helping me out, even though I there was some times the topic was frustrating, but it all became fun now)

Conclusion

Go is very different from other object-oriented languages, it still has its own good sides. Its backed by Google, and is also used by some big companies like IBM, Intel, Medium, Adobe (https://github.com/golang/go/wiki/GoUsers).

I had programming experience before learning Go, most of the concepts wasn't new to me, Go is still very easy to start out with even if you're an absolute beginner.

Want to share how you started out with Go, ask me questions? Let me know in the comments 🙂

Top comments (39)

Collapse
 
bgadrian profile image
Adrian B.G.

Cool! I am learning Go too and I want to share with you guys my resources:

A playlist for newbies,take it as a crashcourse

I wrote a similar article, why I like Go last week

Collapse
 
tingtom profile image
Thomas

Thanks! Just started learning Go myself

Collapse
 
codehakase profile image
Francis Sunday

Thanks for sharing!!

Collapse
 
gcdcoder profile image
Gustavo Castillo

Hello Francis Sunday, I already have some experience in Go (I'm familiar with the syntax), I want to ask you what kind of projects did you build in order to become a better go developer?, I mean did you build APIs, Blogs, Just practice with the language?

Collapse
 
codehakase profile image
Francis Sunday

Whilst learning Go, I built a lot of things, from APIs (Here's an article I wrote on it hakaselabs.github.io/2017/06/23/re...), to little command line applications, currently I've been using Go extensively with the Company I'm with, and also Practice Practice!!

Collapse
 
codehakase profile image
Francis Sunday

You can always get in touch with me if you have any questions on Go, I'd be glad to help :)

Collapse
 
gcdcoder profile image
Gustavo Castillo

Thanks a lot I'm learning and practicing with Go all the time too, but I didn't have the chance of using it in a real project. Cheers!

Collapse
 
robdwaller profile image
Rob Waller

I've started to learn Go, however am struggling with interfaces, particularly compared to how they work in dynamically typed languages like PHP, any tips?

Collapse
 
codehakase profile image
Francis Sunday

Interfaces in OOP, enforce definition of some set of method in the class. By implementing interfaces, you are forcing any class to declaring some specific set of methods.

Interfaces in Go can be seen as named collections of method signatures.

For instance, a Geometry interface in go both Circles and Rectangles can implement the same Interface, but they must implement all the methods of that interface.

package main
import (
  "fmt"
  "math"
)

type geometry interface {
  area() float64
  perimeter() float64
}

type circle struct {
  radius float64
}

type rectangle struct {
  width, height float64
}

// implementing the interface methods

func (c circle) area() float64 {
    return math.Pi * c.radius * c.radius
}
func (c circle) perimeter() float64 {
    return 2 * math.Pi * c.radius
}

func (r rectangle) area() float64 {
  return r.width * r.height
}

func (r rectangle) perimeter() float64 {
  return 2*r.width + 2*r.height
}

// we can call methods that are in the named interface

func measure(g geometry) {
    fmt.Println(g)
    fmt.Println(g.area())
    fmt.Println(g.perimiter())
}

// The circle and rectangle struct types both implement the geometry
// interface so we can use instances of these structs as arguments 
// to measure

func main() {
  r := rect{width: 3, height: 4}
  c := circle{radius: 5}

  measure(r)
  measure(c)
}

Here's an awesome reference to learn more about Interfaces in Go jordanorelli.com/post/32665860244/...

Collapse
 
robdwaller profile image
Rob Waller

Thank you that is really useful.

Collapse
 
casperbraske profile image
Luis

It is said that Go doesn't have "generics" but these look pretty much like generics:

func (r rectangle) area()

func (c circle) area()

Thread Thread
 
baksman profile image
ibrahim Shehu

talking about user defined generics not built in😃

Collapse
 
hooda profile image
Saurabh Hooda

That's a motivating post, Francis.
For the folks who wanna learn Golang, here are community recommended golang tutorials that can be good next step post this article: hackr.io/tutorials/learn-golang

Collapse
 
codehakase profile image
Francis Sunday

Thanks

Collapse
 
kellermanmota profile image
Kellerman

Hello Francis.

Great Article!!

I'm a Java Developer for some years, and I interested in Go for now.
I read in some articles that GO don't have a Virtual Machine behind and all the code is compiled to native code. This is true? If yes, which approach use to manage memory??

Thanks!!

Collapse
 
codehakase profile image
Francis Sunday

Hi there, take a look at this text from the Go website, it'll be of more help to answer your question - golang.org/doc/faq#garbage_collection

Collapse
 
serrevendedora profile image
Revendedora

Great Article!

Collapse
 
foresthoffman profile image
Forest Hoffman

Nice article Francis! Have any thoughts on testing with Go? I had to change the structure of my application in order to write proper tests (which turned out to be worth it). I'm curious to know how your shop does it.

Collapse
 
codehakase profile image
Francis Sunday

Thanks Hoffman, for testing, I use the testing package from the standard go library. In the nearest future I'd like to work on an easy testing framework, say something similar to Jasmine or mocha for Node.js

Collapse
 
itinsidenews profile image
itinsidenews • Edited

Awesome resource, Thank you for sharing. You may also like to check this website out

itinsidenews.com all-time best collection of android, ios and web development tutorials.

Collapse
 
muehan profile image
André

very good article. thanks!

Collapse
 
oparex profile image
Peter Opara

I see what you did there... not cool man, not cool at all, copying other peoples work. zemanta.github.io/

Collapse
 
codehakase profile image
Francis Sunday

Hey Peter, sorry if mine looks similar to yours, this was inspired by that article, I should have placed a credit though, skipped my mind. Anyways I'll make an edit to this.

Collapse
 
oparex profile image
Peter Opara

Hey Francis, please place a credit and link to Zemanta's tech blog in your post. Otherwise no worries, keep teaching others about Go!!
All the best in 2018!

Collapse
 
marvelousubani profile image
Marvellous Ubani

Have been looking to learn Go for a short while now. This is enough motivation for me too start off. Thanks Francis

Collapse
 
codehakase profile image
Francis Sunday

I'm glad this helped!

Collapse
 
johannesvollmer profile image
Johannes Vollmer • Edited

Cool. What do you think of nil?

resolve bad practices of others while keeping the good things.

I think optionals are safer than nil. What do you think?

Collapse
 
codehakase profile image
Francis Sunday

Well technically that may be true, but I prefer to stick with nil I often opt-in for optionals though.. but its preference over standards most times for me.

Collapse
 
codehakase profile image
Francis Sunday

Thanks again for another heads up :)

Collapse
 
carl_conton profile image
Carl Conton

Im stuck on css and dont even trying to finish it)

Collapse
 
codehakase profile image
Francis Sunday

Hilarious, remember if you're stuck somewhere, always ask someone, you'd get out of your problems in no time.

Collapse
 
johnsamuelob profile image
John Samuel Obinna

Wow! Awesome. I will give Go a try.

Collapse
 
codehakase profile image
Francis Sunday

Sure, you're gonna love it

Collapse
 
zhikiri profile image
Zhivitsa Kirill

Hey, thanks for great post. I've just found some bugs in first example, you can check my version here play.golang.org/p/D1V6irVthg