Hello World in Go

Hello World in Go Featured Image

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 WikipediaOpens in a new tab. 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 compilerOpens in a new tab.. Just drop the code into the editor and hit run.

Alternatively, we can download a copy of the Go compilerOpens in a new tab.. At the same time, we should probably get a copy of the solutionOpens in a new tab.. 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

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.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

← Previous Post: [#10] [#12]: Next Post →

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts