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

NameTypeDescription
keystringEnvironment variable name.
valuestringTemporary 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"))}
Go Reference