View : 0

12/04/2026 18:16pm

Ep.16 Go and Error Handling & Unit Testing - Fixing Errors and Testing Code!

Ep.16 Go and Error Handling & Unit Testing - Fixing Errors and Testing Code!

#Programming Education

#programming language

#programmers

#Practice programming

#Testing

#Error Management

#Table-Driven Tests

#Unit Testing

#Error Handling

#Golang

Go and Error Handling & Unit Testing - Fixing Errors and Testing Code!

 

Errors are an inevitable part of programming, and in Go, we have a systematic way to handle them to ensure that the code runs correctly and stably!

 

What is Error Handling?

Error handling in Go focuses on checking and managing issues that may arise during program execution, such as failed database connections, missing files, or incorrect calculations.

Example of Error Handling in Go :

In Go, we often return an error in functions to check if an error has occurred in the code:

The divide function checks if b is 0. If it is 0, it returns an error.

We use if err != nil to check for errors before using the result.

package main

import (
    "errors"
    "fmt"
)

func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("cannot divide by zero")
    }
    return a / b, nil
}

func main() {
    result, err := divide(10, 0)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Result:", result)
}

 

Creating Simple Custom Errors

In addition to general errors, we can also create our own specific errors using ErrInvalidInput, which helps us differentiate between various types of errors more clearly.

var ErrInvalidInput = errors.New("invalid input provided")

func checkInput(input int) error {
    if input < 0 {
        return ErrInvalidInput
    }
    return nil
}

 

Unit Testing in Go

Unit testing is the process of testing small parts of a program to ensure they work as expected. In Go, we have the testing package for writing tests.

An example of testing the add function in this code:

TestAdd is the test function that uses t *testing.T for testing.

t.Errorf is used to report an error if the result does not match the expected outcome.

package main

import "testing"

func add(a, b int) int {
    return a + b
}

func TestAdd(t *testing.T) {
    result := add(2, 3)
    if result != 5 {
        t.Errorf("Expected 5, got %d", result)
    }
}

 

Table-Driven Tests (Testing Multiple Cases at Once)

Table-Driven Tests is a technique that allows us to test multiple cases in a single function efficiently.

In this code:

We create a table with different test cases and then use a for loop to run tests for each case.

func TestAddCases(t *testing.T) {
    cases := []struct {
        a, b, expected int
    }{
        {1, 2, 3},
        {2, 3, 5},
        {0, 0, 0},
    }

    for _, c := range cases {
        result := add(c.a, c.b)
        if result != c.expected {
            t.Errorf("Expected %d, got %d", c.expected, result)
        }
    }
}

 

Running Unit Tests in Go

Use the command go test to run the tests.

go test

 

In Summary

  • Use errors in functions to check for problems.
  • Use testing for function testing.
  • Use Table-Driven Tests to test multiple cases at once.