Sobes.tech
Junior — Middle

How is code testing performed in Go?

sobes.tech AI

Answer from AI

In Go, the testing process is built into the standard library and is very easy to use. To write tests, files with the suffix _test.go are created, in which functions with the signature func TestXxx(t *testing.T) are defined.

Example of a simple test:

package mypackage

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("Add(2, 3) = %d; want 5", result)
    }
}

Tests are run with the command:

go test ./...

You can also use benchmarks and examples, which help check performance and demonstrate function usage.

Thus, testing in Go is integrated and does not require third-party tools.