Hello world in Go
Writing your first "Hello World" program in Go programming language.
Every programming language career starts by writing Hello World
program. Lets do it in Go.
Create a new folder called
hello-world
Create a new file in it called
hello.go
mkdir hello-world
cd hello-world
touch hello.go
- Now write following code in it
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Run the program with following shell script go run hello.go
.
Writing your first test
Go comes with built in test setup, this is a big deal if you are coming from other programming language where you need to setup your test, choose from different testing frameworks.
But how do we test the above code? Before writing a test case it is a good practice to separate your domain
logic from the outside world (side effects). So lets do that first.
// main.go
package main
import "fmt"
func Hello() string {
return "Hello, world!"
}
func main() {
fmt.Println(Hello())
}
func
keyword is used to create a function in Go. we named our function Hello()
and we said this function should return string
. Because Go is strongly typed language, this will helps us catch errors in compile time and our editor will help us fix them before even executing the program.
Lets create another file called hello_test.go
and start writing our first test for our Hello
function.
package main
import "testing"
func TestHello(t *testing.T) {
got := Hello()
expected := "Hello, world!"
if got != expected {
t.Errorf("expected %q got %q", expected, got)
}
}
Few conventions in writing tests
Test file name should
xxx_test.go
in our casehello_test.go
The test function must start with the word
Test
in our caseTestHello
The test function takes one argument only
t *testing.T
In order to use the
*testing.T
type, you need to importimport "testing"
.
Please note that, In the Go Installation and IDE Setup section, you'll find detailed setup instructions for both Visual Studio Code and Neovim. Once your IDE is configured correctly, it can efficiently handle the automatic import of necessary modules, simplifying your development process.
Go modules
Now that our test is ready you can run your test by typing go test
in your terminal
$ go test
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
If you see go.mod
related problem, that means you are running go 1.16 or later, Go has a new module changes, you can read more about it Go Modules.
All you need to type is go mod init hello
in your terminal followed by go mod tidy
to fix this problem, and it will generate a go.mod
file in your project.
$ go mod init hello
go: creating new go.mod: module hello
go: to add module requirements and sums:
go mod tidy
$ go mod tidy
If you are interested in what is generated in go.mod
here is the content:
module hello
go 1.21.5
Now run go test
command again and you will see your test is passing
$ go test
PASS
ok hello 0.001s
Next we will try to follow TDD approach to improve this hello world program.