August 3rd, 2024

Full Introduction to Golang with Test-Driven Development

The article presents a tutorial series on learning Go through Test-Driven Development, covering the creation of a "Hello, World!" program, module management, and testing using Go's framework.

Read original articleLink Icon
Full Introduction to Golang with Test-Driven Development

The article introduces a tutorial series on learning Go (Golang) through Test-Driven Development (TDD). It begins with creating a "Hello, World!" program, emphasizing the TDD cycle, which includes adding a test, running tests, writing code to pass the tests, and refactoring. The author suggests creating a project folder named "go-sandbox" for experimentation and explains the importance of the "go.mod" file for module management in Go projects.

The tutorial outlines the steps to create a package called "helloworld" and a Go file named "helloworld.go" where the function "HelloWorld" will be defined. This function is intended to return the string "Hello, World!". The article stresses the need to write a test for this function in a separate file named "helloworld_test.go".

The test function, "TestHelloWorld", will check if the output of "HelloWorld" matches the expected string. The author explains variable declaration and assignment in Go, using a short variable declaration for storing the function's output. An if statement is introduced to compare the actual output with the expected output, and the article concludes with a brief mention of using Go's testing framework to report test results. This structured approach aims to facilitate learning Go while reinforcing programming concepts through practical application.

Link Icon 6 comments
By @karolist - 2 months
This article is too basic and does not introduce anything you'd encounter in a typical Go project. If you want introduction to Go testing I recommend just reading the official docs https://pkg.go.dev/testing and understanding how to write table driven tests - https://go.dev/wiki/TableDrivenTests.

Going beyond what's built in, get familiar with https://github.com/stretchr/testify as that's used a lot.

By @glottis - 2 months
I like to recommend the excellent learn go with tests [0] that covers both the language and TDD concepts.

[0]: https://github.com/quii/learn-go-with-tests

By @LudwigNagasena - 2 months
That doesn't look like full introduction. That looks more like lecture notes to the lecture on debugging and testing of CS 101. Not even the full lecture, more like the note taker lost interest and fell asleep at the middle.
By @bnormative - 2 months
It is funny how tutorials are always built on top of "hello world" or basic math functions where you don't see anything close to real world. I mean, I would not expect to see production level code but at least it could be a sort of demonstration of it.