DEV Community

Cover image for Go Hello World! Program
Saravanan Gnanaguru
Saravanan Gnanaguru

Posted on • Edited on

Go Hello World! Program

Table of Content

Introduction

As part of Go Tutorial series, we have gone through the steps to install and setup Go development environment in my previous article.

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
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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"
)
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Build and Run

Initialize the module file

  • Run the command below to initialize the module and prepare to execute
go mod init
Enter fullscreen mode Exit fullscreen mode

Build code and Run executable

  • Run the command below to build the code
go build main.go -o helloworld
Enter fullscreen mode Exit fullscreen mode
  • This command generates an executable file helloworld in the current directory.
  • Execute the executable helloworld. It print the value,
Hello World!
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
    }
}
Enter fullscreen mode Exit fullscreen mode

Execute the Unit Test

  • Run the below command to execute the Unit test for main.go
go test
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Go unit testing

Feel free to follow me on various community platforms.
Github
LinkedIn
StackOverflow

Top comments (0)