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
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. Butmain
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 theHello World!
string to the console. - Below is the
import
code 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
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 asmain_test.go
to unit test themain.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 ourmain.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 inmain_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
Go unit testing
Feel free to follow me on various community platforms.
Github
LinkedIn
StackOverflow
Top comments (0)