Go Function Generator
Describe a function and get idiomatic Go: an exported or unexported name, an optional method receiver, context.Context as the first parameter, parameters of the same type grouped, results with error last, a doc comment that starts with the name, argument checks returning errors, and a matching table-driven test in a separate _test.go file.
- Runs in your browser
- No sign-up
- Free to use
How to use Go Function Generator
- Enter the package, function name and optional receiver.
- List the parameters and the results.
- Choose error, context, validation and test options.
- Copy the function and the test file.
Go Function Generator features
Go conventions
Doc comment starting with the name, ID and URL initialisms.
Errors
error as the last result, zero values for the others.
Context
ctx context.Context first, with a cancellation check.
Receivers
Methods on value or pointer receivers.
Table-driven tests
A _test.go file with a test table and t.Run.
Imports
context, errors, time, testing and reflect as needed.
When to use Go Function Generator
- Scaffolding service methods with context and errors.
- Starting functions together with their tests.
- Learning Go naming and error conventions.
- Keeping a team’s function layout consistent.
Go Function Generator FAQ
Why is context.Context the first parameter?
It is the Go convention for functions that may block or call other services: callers pass deadlines and cancellation, and the function should stop when ctx is done.
Why is error the last result?
By convention, the error is returned last; when it is non-nil, the other results are zero values and should be ignored.
What is a table-driven test?
A test that loops over a slice of cases, each with inputs and expected outputs, running each as a named subtest with t.Run. It is the standard style in Go.
What happens to default values?
Go has no default parameters, so defaults are ignored and the notes suggest an options struct or functional options.
How are names handled?
Exported names start with a capital letter. Parameter names become camelCase with Go initialisms, so customerId becomes customerID.
Is anything uploaded?
No. The code is generated in your browser.
Idiomatic Go functions
Go is known for its strong conventions, many of them enforced by tools such as gofmt, go vet and golint. Exported names start with a capital letter, doc comments begin with the name of the thing they describe, errors are returned as the last result and context.Context comes first. Code that follows these conventions reads the same across the whole ecosystem.
The generator turns a short description into such a function. Parameters are entered with neutral types that map to Go: string, int, float64, bool, time.Time, slices, maps and pointers for optional values. Consecutive parameters of the same type share it, as gofmt-style code does, and names use Go initialisms such as ID and URL.
Error handling is explicit in Go. When the function returns an error, the generated checks return a descriptive error from errors.New together with zero values for the other results, and a context check returns ctx.Err() when the caller has cancelled. The notes recommend wrapping underlying errors with fmt.Errorf and %w so callers can inspect them with errors.Is.
Methods are created by giving a receiver such as s *Service. Pointer receivers are the usual choice for types that hold state or are large; value receivers suit small immutable types.
Tests live next to the code in a file ending in _test.go. The generated table-driven test declares a struct with the inputs and expected results, loops over the cases with t.Run so each has its own name in the output, and compares results, using reflect.DeepEqual for slices and maps. You only need to fill in the cases.