Hello World, In Go!

29th Feb 2024

2 mins read

I remember back in my University days, I used to wonder, why is that we have to learn hello world followed by object oriented programming and conditions in each programming unit that i had under my course?

Why not just do one language comprehensively? Until I started my first professional job, and quickly did I realize that whatever we learnt at school was about to make a huge difference in how I perceive things, to build a fully pledged application, I needed the skills earlier taught, I needed to disern where to use which type of loop and so on.

This is to say, writing a simple program like 'Hello World' may seem insignificant, but remember, drop by drop fills a pot.

Now, Make your fingers dirty with me. 

Open your code editor of choice and open your terminal and type:
go mod init demo 

This creates a file go.mod in your project folder which will contain and store your project dependencies.

Go ahead and create your go.main file. 

Each go program requires an entry point, therefore, we will create a package called main in our go.main file. Every file created in go must have this package, hence, it will imported as package filename in subsequent files.

The entry point in this file is a function which is written as func function name. In our case, we will have func main.

We will then have package called fmt which allows us to format strings.
And now, we write our Hello world inside the function using fmt.Println("Hello, World")

By now, you have an idea of what to do and what to write, you may or may not know how to to piece everything together, but for the benefit of doubt, let me write it down to completion.

go.main file

package main

Import "fmt"

func main () {

fmt.Println("Hello, World")

}

Now let's run this program in the terminal.
go run .

The period at the end of the command tells the terminal to run the project in that particular directory.

Cheers! Our first Go program