/ GoLang

Go - Hello World!

Go programs are very similar to any other languages out there. For example It needs a main function, and that is the entry point, basic concept of an executable has not changes yet, it required an entrypoint and a way to pass on arguments via cli or environment variables.

Today we are going to create a simple program that prints Hello <name>/World!!. The requirement is pretty simple, program will accept a param called name, this param will default to world.

Create an empty directory touch main.go, open the file and type in

package main

import (
	"fmt"
    )

func main() {
    fmt.Println("Hello, world")
}

As I said, its pretty simple and straight forward, we have a main function and a Println for printing the message. To run this do
go run main.go, You should see something live this:

Snip20180805_1

So, let talk about the structure of the program. Each file in go belongs to a package, if it is an executable package, it must be called main and it must have main func as well and that is why our package is called main.

By convention, each package must reside in a directory with the same name with an exception to main package, its parent directory can be anything. Also all files in a given directory must have same package name otherwise compiler will through errors. Println is in fmt package, its part of the standard library so we import is as fmt, but if it were a custom package, you'll need to import full relative path from $GOROOT's src. For example, you want to import mux which is hosted at https://github.com/gorilla/mux, first thing you'll need to do it go get github.com/gorilla/mux, this command will clone the repository at $GOROOT/src/github/gorilla/mux and then you can import it like import github/gorilla/mux. Don't worry we are going to deal with such packages in next article where we will create a simple restful service.

Well, that was easy. Now lets add params. To accept params from cli we are going to use a package called flag. The usage is very simple, first we declare the param name, its type and default values. Following code reads param named name from the cli.

package main

import (
	"flag"
	"fmt"
)

// Define the flag. First argument is name of the param, default values comes second - here as the param has to default to world and lastly the description of the param.
// This will return a string pointer which will hold the value after parsing.
var name = flag.String("name", "world", "name defaults to world")

func main() {
    // Parse the flags, if values were provided from command line
    // they will be reflected by the variable `name`
    flag.Parse()
    fmt.Println(fmt.Sprintf("Hello, %s", *name))
}

Lets break it down a bit, first we declare the flag like var name = flag.String("name", "world", "name defaults to world"), call to flag.String indicates that the param is string, we defined name of the param and its default value. flag.String will return a pointer to string (*string). Next we parse the flag when program starts and we print the message using Sprintf to concat string.

We are almost done, the only thing I don't like about this is I'm parsing the flag in main function. Its okay to do so, but there is a more elegant way. Go has a special function (like main) called init. This function will be invoked by the go runtime when the package is loaded, it will be called before main. So we can move flag.Parse to init and make out code a bit cleaner.

package main

import (
	"flag"
	"fmt"
)

var name = flag.String("name", "world", "name defaults to world")

func init() {
	flag.Parse()
}

func main() {
   fmt.Println(fmt.Sprintf("Hello, %s", *name))
}

Thats it.

Full code is available at github

In next article, we will create a hello world json based web api using mux.

Dave Amit

Dave Amit

Howdy folks! I am Dave Amit, an accidental programmer, father to a lab puppy, hubby to a beautiful wife, addicted to puzzles & a noob blogger. This is my effort to simplify odd codes from the wild.

Read More