Software programming has ever been evolving right from the era of humans having to write pure machine code up until now that there are programming languages with syntax almost as straightforward as the English language. Building software applications and solutions that work has gone from the job of so-called geniuses to what any curious person can take on and do in recent times. In this article, I'll be making an introduction of one of the most powerful programming languages of "recent times" created to further improve the ease and speed of building efficient software applications, the Go programming language.
Introduction & History of Go
Go is a statically typed, concurrent, compiled, and garbage-collected programming language designed in 2007 by a team of three engineers at Google - Robert Griesemer, Rob Pike and Ken Thompson. According to Wikipedia, the team came together to design Go, motivated by their shared dislike of C++ as a programming language. They looked to create a programming language that offers the speed and efficiency of C++ with a much lower level of complexity and learning curve compared to it. Go is an open source project and it is cross-platform.
The gopher mascot - Go's official mascotThe technology of the world is in an era that makes heavy use of large and at the same time highly efficient software applications, this has led to the rise of concepts like concurrency, parallelism, multi-core computing, and many more over the years. The need for more programming languages that offer these capabilities while also being easy to learn and use only keeps on increasing every day, it is therefore relatable the reason Go was created.
Go is also often referred to as Golang, but this is only as a result of the language's former website's domain name (golang.org). The current website can be found at go.dev
Go Programming Paradigms
Go was designed based on C++, Java, and Python. It combines the power of these three languages into its native modules, and as a result of this, Go also supports multiple programming paradigms, including:
- Object-oriented programming: Although not a strictly object-oriented language, Go can represent data in form of objects, using structs and not classes like C++ or Java does. It also has support for the 4 main building blocks of object-oriented programming: Encapsulation, Abstraction using interfaces, Inheritance and also Polymorphism.
- Functional programming: Functional programming is a paradigm in which functions are treated as first-class citizens. It is a rather declarative than imperative paradigm in which functions are used to map values to each other. The Go programming language is not a bad idea at all for functional programming as it doesn't have any restrictions to declaring functions and using them in different ways across different parts of a program.
The list of programming paradigms supported by Go still goes on as Go supports imperative programming, procedural programming, and it also has built-in support for concurrent programming. "Guess you're now starting to see the power of Go🦾".
Go Syntax
Programming languages are often identified by their syntax. Although two or more languages may look very much alike syntactically, there are always a few key entities to make the difference. Go is identified with a syntax similar to that of C but it is very much simpler than C. There isn't more than one standard to Go, hence it is easy for any Go programmer to pick up any code base and continue working with it. For a picture of what Go syntax looks like, have a look at the "Hello, 世界" (Hello, World) program below:
// a simple hello world program in Go
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界") // print Hello, 世界
}
To run the above code locally, you need to have Go installed on your machine, or you could use the official Go online playground. The output of the above code is obviously Hello, 世界
. Go can process and output the Chinese characters correctly due to its built-in support for unicode. Therefore, it can handle text written in any language in the world. Before going over each line of code in the program above, you might be interested in running the code locally. To do this, install Go from the official website if you haven't, then create a new file in any editor of your choice and name it anything you wish with a .go
extension e.g hello_world.go
. Save the file and run the following command in your terminal: go run <your_filename>.go
where <your_filename>
is whatever you named your file. The command compiles your program and runs it right there in your terminal. If all went well, the output: Hello, 世界
will be displayed in your terminal. That was easy!
Now to the sample program. The first line is a comment. Just like many other languages, comments in Go are identified by lines starting with double backslashes \\
and aren't considered during compilation. The next line package main
means to declare the start of a new package in Go, it is required at the beginning of every file, and every type, function, or method defined inside the file becomes a part of the package declared. To access functions or methods of a package in another source file/package, the import
keyword is used to call the package and its functions into the other source file. This explains what the next line: import "fmt"
does. It makes functions of the fmt
package available for use in the main
package's source file, and this can be seen inside the main function where the Println()
function was called from fmt
to print the text to standard output (i.e fmt.Println("Hello, 世界")
). The func
keyword there, is the keyword for defining functions in Go. The Go standard library is very rich in packages, the list goes from basic I/O packages to media processing packages and even web development packages for building servers and APIs.
Applications of Go
Bearing in mind that Go was built to do the things C++ could do without having to be as complex as C++, Go was built for software that had to do with networking, infrastructure, and core server programming. But over the years, due to its power and simplicity, it has seen adoption in diverse fields of software development, it is now one of the top choice languages for Server-side web development, CLI tools, Cloud applications, Blockchain development, as well as DevOps and so much more. Some popular technologies built with or which make use of Go include Docker, Slack, SendGrid, Twitch, Uber, SoundCloud, and of course, Google as a whole makes heavy use of Go in its codebases.
Go is widely-used across the global industryCharacteristics of the Go language
Go is majorly characterized by its massive speed, memory safety, and simplicity. It doesn't only do its job as a programming language, it does enough to make the life of a programmer very easy. It is safe to say Go does well to relieve a programmer from the stress of having to look up third-party libraries and tools. Go comes with a bunch of components and utility tools to help a programmer's productivity. These tools include Gofmt for automated code formatting, Goget for installing external libraries easily, Godoc for looking up the Go documentation right from the command line, etc. That's not all, functions in Go can return multiple values of multiple types at once. There are other features like Go routines for concurrency, Go functions for asynchronous programming(not to be mistaken with functions in Go), Garbage collection(that doesn't strip the programmer of the ability to manually manage memory with pointers), and many more. All of these make development with Go very friendly.
Common characteristics you may not find in Go
As packed as the Go library is as of writing, there are some features common to almost all programming languages that Go lacks. Some of these features include: A generic variable type keyword (e.g var
in JavaScript & auto
in C++), pointer arithmetic, function & operator overloading, ternary operator conditional statements(?:
), implicit numeric type conversions, GUI library, algebraic variables, etc.
Another fun thing you won't find in Go is exception handling. The generic try-catch
system is not valid in Go programming, you could even find Go programmers saying: "Go handles exceptions by not having exceptions". How then does Go handle exceptions? Find out here. Go has alternative approaches to handle most of the features it lacks, in which some of these approaches are even easier to implement and more efficient.
Top resources to Learn Go
While there are so many resources available on the internet to learn how to build applications with Go, an unordered list of some recommended resources is given below:
- The official documentation
- Golangdocs.com
- Effective Go (Also an official resource)
- Golang Cafe on youtube
- The Go programming language e-book
- Gobyexample.com
- A tour of Go - An official resource that lets you learn interactively
- Gophercises - A free course by John Calhoun
Conclusion
So far in this article, from history to shortcomings, it's been all about the Go programming language and how it could be useful in different areas of software development. It is one of the top languages to learn at this time, and I would even recommend it for a complete beginner in programming. You could get started with Go with any of the resources listed above and get to discover so much more about the language while building something cool 😎. Questions are also welcome in the comment section. See you in the next article🥂!