Why does the testing library use pointer instead of value receivers?

Been reading about when to use a pointer or value receiver and I think I know most of the reasons for making the decision but, now I’m curious about this specific example:

// Example of using the testing library
func TestHelloName(t *testing.T) {
	name := "Gladys"
	want := regexp.MustCompile(`\b` + name + `\b`)
	msg, err := Hello("Gladys")
	if !want.MatchString(msg) || err != nil {
		t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)
	}
}

I would LOVE to know the reason pointer receivers were chosen for the testing library- I think this would be helpful for my understanding. Did they chose a pointer because large structs could be passed to it? Or is there something inherent to how the testing library is used that makes pointer receivers the right choice here?

Because it contains fields it mutates fur some calls. Most prominent is probably the muted it holds.

Why does it have to mutate some variables for certain methods? just want to understand the whole use case better

Mutexes are usually used to avoid writing from many go routines to the same memory address and therefore producing races.

Also, as from a test function noting is returned, but instead methods of the test “objects” are called, it has to keep the results stored in itself.

1 Like