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/srcdirectory. - 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.gofile.
Package
- Every go program starts with a
packagedeclaration.packageis nothing but the modules for doing specific operation. -
packagestatement should be first executable line in Go program.
package main
-
package mainis the entry point module for Go programs. We can have as many number of packages in our project. Butmainpackage 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 theHello World!string to the console. - Below is the
importcode snippet
import (
"fmt"
)
Function main
- Since go is inspired from
C++andJava, we need to declaremain()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
printfunction, 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
helloworldin 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 runcommand, - 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
testingto 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.gosource file. So create test file asmain_test.goto unit test themain.gofunctionality.
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,
-
packagestatement -
importstatement func main()
-
Go Unit Test Program
- Below is the unit test
main_test.gocode for ourmain.gosource 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.gofile and runs the test cases coded inmain_test.gofile. - 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
Go unit testing
Feel free to follow me on various community platforms.
Github
LinkedIn
StackOverflow
Top comments (0)