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
}
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)
}
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)
}
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")
}
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!!")
}
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.
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)
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
Thanks! Just started learning Go myself
Thanks for sharing!!
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?
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!!
You can always get in touch with me if you have any questions on Go, I'd be glad to help :)
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!
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?
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.Here's an awesome reference to learn more about Interfaces in Go jordanorelli.com/post/32665860244/...
Thank you that is really useful.
It is said that Go doesn't have "generics" but these look pretty much like generics:
func (r rectangle) area()
func (c circle) area()
talking about user defined generics not built in😃
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
Thanks
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!!
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
Great Article!
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.
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.jsAwesome 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.
very good article. thanks!
I see what you did there... not cool man, not cool at all, copying other peoples work. zemanta.github.io/
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.
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!
Have been looking to learn Go for a short while now. This is enough motivation for me too start off. Thanks Francis
I'm glad this helped!