Standard library / testing
testing.B.Cleanup()
Register cleanup callbacks in last-in-first-out order after this scope finishes. Useful for temporary resources. Expected process exit: 0; output contains ['PASS', 'body reached', 'second cleanup\nfirst cleanup']. Run as a _test.go file using the Go test harness.
Formal Syntax & Structure
go syntaxgo
func (c *B) Cleanup(f func())Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| f | func() | Function under test or callback to run. |
Working Example
Example Snippetgo
package main
import ( "fmt" "testing")
func BenchmarkReference(b *testing.B) { b.Cleanup(func() { fmt.Println("first cleanup") }) b.Cleanup(func() { fmt.Println("second cleanup") }) fmt.Println("body reached")}