Standard library / testing
testing.B.Setenv()
Temporarily set an environment variable and restore the previous value on cleanup. Environment mutation is process-wide; avoid parallel use. Expected process exit: 0; output contains ['PASS', 'value demo']. Run as a _test.go file using the Go test harness.
Formal Syntax & Structure
go syntaxgo
func (c *B) Setenv(key, value string)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| key | string | Environment variable name. |
| value | string | Temporary environment variable value. |
Working Example
Example Snippetgo
package main
import ( "fmt" "os" "testing")
func BenchmarkReference(b *testing.B) { b.Setenv("BYTEMIND_DEMO", "demo") fmt.Println("value", os.Getenv("BYTEMIND_DEMO"))}