Welcome back to another episode of Hello World in Every Language. For our ninth language, we’re going to try to implement Hello World in Go, a language from Google.
Table of Contents
Go Background
Once again, I took to Wikipedia to learn more about today’s language, Go.
According to Wikipedia, Go, also known as golang, is a language that was developed in 2009 by Google. Apparently, Google was looking to create a language that kept the good parts of major languages while eliminating the bad parts. In fact, the designers revealed that their primary motivator was their disdain for the complexity of C++. I think I can relate.
As a result, Google decided to model their new language after C++’s predecessor, C. This allowed them to design a procedural language without all the complexity of classes and the verbosity of mandatory keywords. In addition, Google was able to incorporate features they felt were improvements over C such as coroutines and garbage collection. Sounds like a language I’d like to explore.
As a small tangent, I’ve noticed that every language we’ve covered has been labeled a general-purpose language by Wikipedia – except this time. As a result, I felt compelled to dig a little deeper to find out what role Go fits into. It appears that Go is a great language for web servers and command-line tools. I suspect it’s pretty similar to C in that regard, but C is still labeled general-purpose. We can probably categorize Go in the same way.
Hello World in Go
Despite Google’s efforts to limit complexity, they still managed to force a relatively complex implementation of Hello World in Go:
package main import "fmt" func main() { fmt.Printf("Hello, World!") }
Clearly, C had a heavy influence on Go. After all, this implementation is almost identical to our implementation of Hello World in C. That said, there are some differences.
First off, the package line is brand new to us in this series. From what I understand, Go code can be organized into packages. However, if we want to tell the Go compiler that we are creating executable code, we need to use the main package. Otherwise, we’ll be creating a shared library.
Next, we have our import line. At this point, we should be pretty used to importing other packages. In this case, we’re importing the “fmt” into our solution. This package gives us access to several formatted IO functions like Printf.
Finally, we have our usual main function and our call to some print function. However, I want to call out the print function. Notice that it starts with a capital letter. Some of us might find that unusual since Pascal Case is typically reserved for things like classes. In Go, Pascal Case actually a key part of the syntax. All functions that start with a capital letter in a package are exposed to the world. In other words, only use Pascal Case for functions that you want to expose in your API. How’s that for language design?
How to Run the Solution
As usual, the easiest way to run the solution is to use an online Go compiler. Just drop the code into the editor and hit run.
Alternatively, we can download a copy of the Go compiler. At the same time, we should probably get a copy of the solution. Now, assuming that Go is in our path, all we have to do is navigate to the folder of the solution and run the following:
go build hello-world
That should automatically build the script into an executable and run it. If everything checks out, the console should print the “Hello, World!” string.
Sample Programs in Every Language
That’s it for Hello World in Go! Up next, I’m looking to play around with Rust, a language sponsored by Mozilla. After Rust, I’d like to get back to some of the more popular languages like PHP and JavaScript.
As always, if you liked this article, don’t forget to share it with your friends. Also, feel free to recommend the next language in the comments.
Recent Posts
One of the core features of modern programming languages is functions, but did you know Python has them too? Let's take a look!
Python has a cool feature that allows you to overload the operators. Let's talk about what that means and how you might use it!