An Introduction to Go with “Hello World”(Go)

Hello World in Go

This week I started looking at Go and as I am new to Go, decided that a few blog entires was needed to help me learn and remember details.

Pokemon Go, which won't get another mention in this Go article.
Pokemon Go, which won’t get another mention in this Go article.

History
Go, also known as Golang, is a programming language that started as an idea in 2007 and saw its first release in November 2009, making it 10 years old this month. It was created by Robert Griesemer, Rob Pike and Ken Thompson. Go was created as a compiled language looking to compile fast.

What’s it like?
From my adventures so far with Go it is very similar to C and Java, so anyone that has experience with those languages should feel at home with Go. Go is cross platform, strongly typed and a compiled programming language, containing less than 30 keywords.

Go files have the extension .go and can be compiled using the command go build on the command line after installing a Go compiler. The Go compiler is very easy to install and is available from https://golang.org/dl/ for Windows, Mac and Linux.

What’s it look like?

Hello World in Go
Hello World in Go

A “Hello World” example in Go could look like this:

package main

import “fmt”

func main() {

               fmt.Printf(“hello, world\n”)

}

I’ve placed the key words in blue to help this stand out. The first word, package, creates the main package. This line tells the Go compiler that the program is needs to be compiled as an executable and not as a shared library.

The import word tells Go which modules are to be imported for use. In this case fmt is imported. fmt is used to create and show formatted input/output (e.g. text on the screen). Modules can be imported individually (e.g. import “fmt”, import “os”) as in this example or can be imported en masse via:

import (

“fmt”

“os”

)

Which would save you having to type the word import multiple times.

The func key word then creates a function, in this case called main(). The main() function is then defined within the { curly braces }. The main() function in this example called on fmt and then uses fmt’s Printf (print formatted) option to print (i.e. output to screen) the words “hello, world” and the \n is used to create a new line at the end of the words. Instead of Printf I could have used Println which would print line.

Note: Go is case-sensitive. Printf is not the same as printf, and vice-versa.

Saving Go Files And The Go Workspace

Go uses a workspace to save its files. This workspace is composed of 3 folders:

  • bin
  • pkg
  • src

The src folder should then contain a sub-folder with the name of the program, and inside this folder should be the program with the .go file extension. This workspace can be saved into an operating system as a system variable called GOPATH.

Comments

geektechstuff_go_hello_world_comments
Example of Go comments

Go has line comments, which need to be opened with // and block comments which need to be opened with /* and closed with */

I have included an example of both comment types in the above screen grab. If you need a single line comment, then line comment is best to use. If the comment is going across multiple lines then block comment is more for you.

 

Posted in Go

One thought on “An Introduction to Go with “Hello World”(Go)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.