Hello world in Golang

Hello world in Golang

Table of contents

No heading

No headings in the article.

Hi there ๐Ÿ‘‹

Welcome back to my blog.

In today's episode, I will explain some code blocks found in my "Hello World" program in Golang. Documenting my own learning journey in form of writing is my own way of staying accountable to this new journey I started ๐Ÿ˜Š

Let's go...

So after installing both Go and VSCode on my computer and configuring it, I wrote my first program and then was able to use the same method to write many other messages. By the way, if you are also learning to Go just like me, here is a great place to start: https://go.dev/learn

My first "Hello World!" Program

package main

import "fmt"

func main() {

fmt.Println("Hello World!!")

}

Here is what the code looked like on my VSCode and the screenshot below is the output on my terminal.

Yaaassssssss! I wrote my first go program and I am super excited. I am happy because while doing this, I got stuck and could not unstick myself for over two weeks. The easy way out for me since I am just a beginner was to create a new file and start all over again. I did that and it worked which means I am back on track now.

Now, what does the whole package main func etc. words going on in my VSCode mean?

Some Go commands you should look out for:

go run compiles and immediately executes one or two files. Code example: go run main.go the name of the file here is main.go

go build compiles a bunch of go source code files and does not execute it. Code example: go build main.go will only build the program in your terminal and not display any output

ls this is a terminal command that lists out all the files in your directory

clear this is also a terminal command that helps you clear your terminal so you can execute new programs

go fmt Formats all the code in each file in the current directory

go install Compiles and "installs" a package.

go get Downloads the raw source code of someone else's package

go test Runs any tests associated with the current project

Go Packages

package main what does it mean?

Think of a package as a project or a workspace that collects common source code files. It doesn't matter if more than one person is working on a particular project, they will all be using the same package

A package can have many files inside of it which must always end with the file extension .go but the only requirement is that the very first line of each file must declare the package that it belongs to. In this case, package main just as in the screenshot above.

Why main and not firstgoprogram?

In Go, there are two different types of packages namely:

  1. Executable: when compiled, this package generates a file that we can run (an executable file).

  2. Reusable: these can also be referred to as code dependencies or libraries. It helps us type in some reusable logic or helper functions that help us reuse some codes in future projects.

Now, how do we know if we are making an executable package or a reusable one?

It's simply the name of the package used that determines this. Specifically, the word main is used to make an executable-type package. You can read more about package names here. In summary, any time we see the word package main it means we are making an executable package and any other name used means we are making a reusable or a dependency type package e.g package calculator package uploader

Import Statements

import "fmt"

The import statement is used to give the current package we are working with access to the code that is being written in another package. E.g when this command is written import "fmt" it means to give my package main access to all the codes and all the functionality that is contained in the package fmt which is just the name of the standard library package that is included in the go programming language by default. It's a short form for the word "format".

File Organization

func main() {

}

more context on this...

func tells go we're about to declare a function

main sets the name of the function

() list of arguments to pass the function

{ } the space in between is the function body. Calling the function runs this code

These lines and examples pretty much explain what's going on in my block of code in the screenshots above. I hope this made a lot of sense.

Just for fun and using the same pattern, I was able to write and execute other programs.

My subsequent articles will be explaining certain concepts in go, how I learned them, and basically code blocks to show how they work.

Thanks for reading my blog, cheers!

ย