Weekend Coding Kata Challenge – Fizzbuzz TDD Using Golang

My work colleagues decided to go with Golang stack for a new repo we are about to start. We did a mob session of FizzBuzz kata using Golang stack. It was really fun. I decided to finish off the kata by doing my own refactoring, and adding some more tests.

  • Golang looks really good. I can’t wait to get started. I hope all the libraries we will need are available. Fingers crossed :D.
  • I always enjoy having a ternary operator available. I should check out why such an operator is not available in Golang.
  • I’m keen to explore other katas that can show me how to do more advanced stuffs using Golang

Here is the link to the repo:

https://github.com/suryast/fizzbuzz-go

package main

import (
	"log"
)

func fizzbuzz(input int) string {
	if (fizz(input) + buzz(input)) == "" {
		return strconv.Itoa(input)
	} else {
		return fizz(input) + buzz(input)
	}
}

func fizz(input int) string {
	if input%3 == 0 {
		return "fizz"
	}
	return ""
}

func buzz(input int) string {
	if input%5 == 0 {
		return "buzz"
	}
	return ""
}

func main() {
	log.Println("beep boop")
}
package main

import (
	"github.com/stretchr/testify/require"
	"testing"
)

func TestFizz(t *testing.T) {
	// Given
	input := 3

	// When
	result := fizzbuzz(input)

	// Then
	require.Equal(t, "fizz", result)
}

func TestBuzz(t *testing.T) {
	// GIVEN
	var input int
	input = 5

	// WHEN
	result := fizzbuzz(input)

	// THEN
	require.Equal(t, "buzz", result)
}

func TestFizzBuzz(t *testing.T) {
	// GIVEN
	input := 15

	// WHEN
	result := fizzbuzz(input)

	// THEN
	require.Equal(t, "fizzbuzz", result)
}

func TestDefault(t *testing.T) {
	// GIVEN
	input := 2

	// WHEN
	result := fizzbuzz(input)

	// THEN
	require.Equal(t, "2", result)
}