Golang Hello World – Writing your First Go Program

Golang Hello World – Writing your First Go Program

The first task is to write a simple program that displays Hello World! in Go. Like most programming language Hello World! illustrates the core structure and how to write the basic syntax in a programming language. It has become a tradition since Brian Kernighan wrote it has part of BCPL documentation.

The entire code structure for the “Hello World!” in Go can be written in a simple or complex way. However, we would adopt the simple method, which has about three main steps.

To write this code open your favorite IDE or Code Editor for Go, See Best IDE for Golang. You can also test it out on Golang Playground, this is probably the easiest way to test the code because you do not need any setup.

Writing Hello World! in Go with Visual Studio Code and Command Prompt (CMD).

On the search button type CMD, then click on command prompt once it appears. Ensure you are on C:\ If you are on C:\Users\yourprofile name type cd .. to go one step backward, do that until you are on C:\

Note: This is a simple practice exercise, Git will not be introduced or used for this example. Git here could be Github, Gitlab, BitBucket or any Git product.

Type mkdir hello to create a new hello directory. now cd hello to change into that directory.

You should see this now.

C:\hello

By now you should have downloaded and installed Visual Studio Code, this is the IDE that will be used for most of this tutorial series.

To open visual studio code from console type code put a space then . has seen below.

c:\hello> code .

This will open the visual studio with the hello go directory.

Now create a new file by clicking on file then new file. Save this new file as hello.go.

Now start typing the code below on the visual studio code IDE also known as vscode.

package main

import "fmt"

func main(){
   fmt.Println("Hello World!")
}

Now, let’s run the new code on the command prompt (CMD). Type in the console.

c:\hello> go run test.go
Hello World!

This will print on the console Hello World! You have just successfully run your first program in Go. To build your binary file, The syntax is different, you need to type.

c:\hello> go build test.go

This will generate a binary file.

For every go project, you must have one package named main and a function main(), this is where the program execution starts from.

Analysis of the Hello World Program Executed

A project can only have one main() and package main. You can name every other folder, function, and package with any name as long as it is not a reserved name. In other words, you cannot have more than one package main and main() function in a single project.

As for the go run command, it creates an executable file temporarily and deletes, after it prints to the console. While the go build command produces a permanent executable file.

Now go to the folder hello, you will notice two files, one is test.go and the other is test.exe. The test.exe is the binary build, you will also notice that the size is bigger than the test.go. This is because the binary build contains the compiler than runs the program, it also includes all the dependency needed by the program to execute the code.

On Windows, you can see this on the console following the code sample below.

c:\hello> go build test.go
c:\hello> dir
test test.go

In the “Hello World!” code example, we import “fmt” this is part of the go standard library.  Also, note that if you import a package and it is not used, whenever go runs the code, it will generate an error. Though there is a syntax that can fix that for you, we cannot rely on it all the time. The goal is to form a good code writing habit and practice. Later we would look see examples where multiple packages are imported. In such instance, the bracket will be used to hold more than one package as seen below.

package main

import (
       "fmt"
        "os"
)

The “fmt” package contains code for dealing with I/O, in simple terms, it is used in the main function to print text to the standard output.

That will be all for now, with Hello World! in Go, this might be a little too detailed for the experienced programmers. For newbies I hope this helps so far, I will refer you to other sections to catch up with some basics of programming.

Coding Resources for Newbies

  1. Git – Github, Bitbucket, and Gitlab
  2. IDE – See Best IDE for Golang
  3. Learn to use the console – CMD
  4. Golang Documentation
  5. Go Playground

Post Comment