test string concatenation in go

To test string concatenation in Go, we can create a unit test that ensures the correct string is returned after concatenation.

Here is an example:

main.go
func TestConcatenateStrings(t *testing.T) {
    str1 := "Hello, "
    str2 := "world!"
    expected := "Hello, world!"
    
    result := str1 + str2
    
    if result != expected {
        t.Errorf("Concatenation failed. Actual: %s, Expected: %s", result, expected)
    }
}
276 chars
12 lines

In this example, we create two strings str1 and str2 which we will concatenate. We expect the result to be the string "Hello, world!". We then use the + operator to concatenate the strings and assign the result to result.

Finally, we compare result with our expected value expected. If the two do not match, we raise an error using the t.Errorf() function. This will stop the test and report an error.

This test function is part of a bigger test suite and can be run with a test runner such as go test.

gistlibby LogSnag