Hello World in Go

Sher Chowdhury
5 min readJul 11, 2020
Photo by Pablo Gentile on Unsplash

There’s already a lot of Go Hello-World articles on the internet. But none of them quite explains them the way I understand them in my head. So here’s my take on writing a hello-world app in Go.

A Go app is made up of one or more text files with the .go extension.

So here I’ve my text editor, VSCode, to create a new folder called demo1, and inside that folder I created my hello.go file:

Hello World in Go

* You can try this out in the Go Playground

Let’s go through this example starting with the first line, package main This line is called the package declaration

The package declaration

In Go, packages are used as a way to organize and reuse your code. All .go files must start with a package declaration. It’s used for telling Go which collection of files makes up a particular package. If the package declaration concept never existed then you would’ve been forced to write all your app’s code into a single .go file.

In addition to that, naming a package “main” also has special meaning in Go. It tells Go that the code in this package is meant to be run as an executable. That is, you can run the code from the command line

$ go run hello.go
hello world!!!

And also you can compile you your code into an executable binary file.

$ go build hello.go$ ls -l
total 2368
-rwxr-xr-x 1 schowdhury staff 1.1M 5 Jul 19:11 hello
-rw-r--r-- 1 schowdhury staff 56B 5 Jul 19:01 hello.go
$ file hello
hello: Mach-O 64-bit executable x86_64
$ ./hello
hello world!!!

You can also move the binary into one of our $PATH folders and then run it like an ordinary command.

$ mv hello /usr/local/bin/
$ hello
hello world!!!

That’s one of the cool things about Go, it lets you easily create your own personal tools, and then run them like any other command in your terminal.

All .go files for a given package name should live in the same directory. If you want to organise your .go files into a more tree-like folder structure, then you should consider setting up your Go project as Go Module. We’ll cover that later.

Packages by any other name are referred to as library packages. These are packages that (among things) offers blocks of reusable code in the form of functions that you can call on from your main package. To use these library packages, you need to first pull them into your project using the import statement.

The import statement

This is used to load in library packages into your project. Importing packages makes various resources available at your disposal, such as functions, constants, variables…etc. You can then use these resources as building blocks for creating your app.

In our example we’ve imported the fmt package. This package has a collection of functions for printing output to your terminal. This package is one of the Standard Library packages. The Standard Library is a collection of packages that comes included with your Go. You can also import third-party packages form elsewhere, such as github, we’ll cover how to do that later.

Functions

A function is a discrete block of code that performs a specific task. A function can:

  1. can accept 1 or more inputs (aka arguments)
  2. run a block of code using those inputs
  3. return or 1 or more outputs (aka return values)

So while the package declaration let’s you organise your code into smaller .go files, functions let’s you organise your code at a lower-level within those .go files, turning them into discrete blocks and also making them reusable.

Functions are declared using the func key word. We’ll cover functions in more detail in a later article.

The main function

The last section of your hello world app is the declaration of a function called “main”:

func main() {
fmt.Println("hello world!!!")
}

A function with the name “main”, has a special meaning in Go. It tells Go what code block it should run first when running your app. Without it, Go would have no way of knowing which function, in which .go file should be executed first. Every main package must have exactly one main function declared.

In our hello-world app our main function only has one line of code, fmt.Println("hello world"). Here we’re telling Go to run the fmt package’s Println function, and providing this function with the argument “hello world”. This function is used for printing out it’s input argument to your terminal’s standard output. When you want to call a function from another package you have to use the {package-name}.{function-name}() notation. This is kind of like how you tell Go where to find a particular function.

Summary

We’ve covered a lot of new ground in this article, so let’s do quick recap of the key points:

  • Go files are plain text files that have the filename extension of .go
  • I recommend writing your Go app using VSCode
  • Each Go file must start with a package declaration
  • The package declaration construct enables you to split your program’s code into smaller Go files
  • The package main package declaration has a special meaning in Go. It tells Go that your app is supposed to be an executable. A package by any other name are treated as library packages.
  • You need to use the import declaration to pull in other library packages, if you want to use them in your app.
  • Functions let’s you organise your code into discrete reusable code blocks within your Go files. You declare functions using the func key word.

Further Reading

--

--