golang/go · error

%s: wrong signature for %s, %s

Error message

%s: wrong signature for %s, %s

What it means

From `checkTestFunc`: a function named like a test entry point (TestXxx, BenchmarkXxx, ExampleXxx, FuzzXxx, TestMain) does not match the required signature, or carries type parameters (forbidden on test functions). The message states the expected form, e.g. `must be: func TestFoo(t *testing.T)`.

Source

Thrown at src/cmd/go/internal/load/test.go:815

			continue
		}
		t.Examples = append(t.Examples, testFunc{pkg, "Example" + e.Name, e.Output, e.Unordered})
		*seen = true
	}
	return nil
}

func checkTestFunc(fn *ast.FuncDecl, arg string) error {
	var why string
	if !isTestFunc(fn, arg) {
		why = fmt.Sprintf("must be: func %s(%s *testing.%s)", fn.Name.String(), strings.ToLower(arg), arg)
	}
	if fn.Type.TypeParams.NumFields() > 0 {
		why = "test functions cannot have type parameters"
	}
	if why != "" {
		pos := testFileSet.Position(fn.Pos())
		return fmt.Errorf("%s: wrong signature for %s, %s", pos, fn.Name.String(), why)
	}
	return nil
}

var testmainTmpl = lazytemplate.New("main", `
// Code generated by 'go test'. DO NOT EDIT.

package main

import (
	"os"
{{if .TestMain}}
	"reflect"
{{end}}
	"testing"
	"testing/internal/testdeps"
{{if .Cover}}
	"internal/coverage/cfile"

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Match the exact signature: `func TestXxx(t *testing.T)`, `func BenchmarkXxx(b *testing.B)`, `func ExampleXxx()`, `func FuzzXxx(f *testing.F)`, `func TestMain(m *testing.M)`.
  2. Remove any type parameter clause `[T any]` from test functions.
  3. Run `go vet` on the package — it flags malformed test functions before `go test`.

Example fix

// before
func TestSum(t testing.T) { ... }

// after
func TestSum(t *testing.T) { ... }
Defensive patterns

Strategy: validation

Validate before calling

// Run go vet on the test package before go test.
func vetTestPkg(dir string) error {
    return exec.Command("go", "vet", dir).Run()
}

Prevention

When it happens

Trigger: Writing `func TestFoo()` (missing parameter), `func TestFoo(t testing.T)` (value not pointer), `func BenchmarkFoo(b *testing.B)` with wrong type, or `func TestFoo[T any](t *testing.T)` (type parameters not allowed on test functions).

Common situations: Forgetting the `*testing.T`/`*testing.B` parameter, copy-paste from non-test code with value receivers, attempting to parameterize tests with generics.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/0a7be151f1ec8f6e. Report an issue: GitHub.