Go Hello World! Program
Tags: go, beginners, tutorial
Published: 2020-07-13 19:04:38

# Table of Content
- [Introduction](#introduction)
- [Go Development Space](#go-development-space)
- [Go Program Structure](#go-program-structure)
  - [Package](#package)
  - [Import statement](#import-statement)
  - [Function main](#function-main)
  - [Function print](#function-print)
  - [Full source code](#full-source-code)
  - [Build and Run](#build-and-run)
    - [Initialize the module file](#initialize-the-module-file)
    - [Build code and Run executable](#build-code-and-run-executable)
    - [Alternate Execute Method](#alternate-execute-method)
- [Writing Go Unit Test](#writing-go-unit-test)
  - [Go Unit Test Program Structure](#go-unit-test-program-structure)
  - [Go Unit Test Program](#go-unit-test-program)
  - [Execute the Unit Test](#execute-the-unit-test)
- [Key Take Away](#key-take-away)

# Introduction
As part of Go Tutorial series, we have gone through the steps to install and setup Go development environment in my [previous article](https://dev.to/chefgs/getting-started-with-go-in-ubuntu-16-04-3b59).

In this blog, let us get started to write a `Hello World!` program in Go.

# Go Development Space
- After installing platform specific go installer. We need to setup $GOPATH environment variable. Refer the [Go Getting Started blog](https://dev.to/chefgs/getting-started-with-go-in-ubuntu-16-04-3b59)
- Go programs should be written inside the `$GOPATH/src` directory.
- Execute the below commands, to get started with `Hello World!` Go program development
```
cd $GOPATH/src
mkdir helloworld
cd helloworld

vim main.go
```

# Go Program Structure
- Let us start writing the `main.go` file.

## Package 
- Every go program starts with a `package` declaration. `package` is nothing but the modules for doing specific operation.
- `package` statement should be first executable line in Go program.
```
package main
``` 
- `package main` is the entry point module for Go programs. We can have as many number of packages in our project. But `main` package is the beginning for Go program execution. 

## Import statement
- Import statement block should contain the modules used in the program.
- We will be using the Go module `fmt`, to print the `Hello World!` string to the console.
- Below is the `import` code snippet
```
import (
  "fmt"
)
```

## Function main
- Since go is inspired from `C++` and `Java`, we need to declare `main()` function in Go as well.
- This function calls the rest of other methods and functions
```
func main() {
  val := print()
  fmt.Println(val)
}
```

## Function print
- Finally we will code the `print` function, to display the text in console output.
- This function has been invoked in the `func main()`
```
func print() string {
  str := "Hello World!"
  return str
}
```

## Full source code
The `main.go` looks like the below
```
// Go Program to print the string "Hello World!"
package main

import (
  "fmt"
)

func main() {
  val := print()
  fmt.Println(val)
}

func print() string {
  str := "Hello World!"
  return str
}
```

## Build and Run
### Initialize the module file
- Run the command below to initialize the module and prepare to execute
```
go mod init
```
### Build code and Run executable
- Run the command below to build the code
```
go build main.go -o helloworld
```
- This command generates an executable file `helloworld` in the current directory.
- Execute the executable `helloworld`. It print the value,
```
Hello World!
``` 

### Alternate Execute Method
- Instead building and running the executable. We can also directly execute the go source file using `go run` command,
- Execute the command after doing `go mod init`
```
go run hiworld.go
```

## Writing Go Unit Test
- Go provides the built-in functionality to test your Go code. Hence, we don't need an expensive setup or 3rd party libraries to create unit tests.
- Go has the module called `testing` to achieve the same.
- So if we want to test a go program, we can create a unit test file with `<source_go_filename>_test.go`.
- For example: We have created `main.go` source file. So create test file as `main_test.go` to unit test the `main.go` functionality.

## Go Unit Test Program Structure.
- Go test program also follows the similar statement pattern as main source go file.
- It should have the below sections,
  - `package` statement
  - `import` statement
  - `func main()`

## Go Unit Test Program
- Below is the unit test `main_test.go` code for our `main.go` source file,
```
package main

import "testing"

func TestHello(t *testing.T) {
    expected := "Hello World!"
    if got := print(); got != expected {
        t.Errorf("print() = %q, want %q", got, expected)
    }
}
```

## Execute the Unit Test
- Run the below command to execute the Unit test for `main.go`
```
go test
```
- This command expects the `_test.go` file and runs the test cases coded in `main_test.go` file.
- The test run outputs the below in console, if the test is passed.
```
PASS
ok      main 0.002s
```

# Key Take-Away
- We have covered the topics below in this blog,
  - Go development basics
  - Develop Go program
  - Go program build and execute methods
  - Creating Go unit test methods

Hope the blog content is useful. 

### Bibliography
[Go By Example-HelloWorld](https://gobyexample.com/hello-world)
[Go unit testing](https://golang.org/pkg/testing/)

Feel free to follow me on various community platforms.
[Github](https://github.com/chefgs)
[LinkedIn](https://www.linkedin.com/in/saravanan-gnanaguru-1941a919/)
[StackOverflow](https://stackoverflow.com/users/4923204/saravanan-g?tab=profile)