Servirtium for
Go
No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.
- FFI mechanism
- cgo. The module cgo-links libservirtium_vcr at build time (-lservirtium_vcr), not via a runtime dlopen. You supply the library directory through CGO_LDFLAGS (-L plus a baked-in rpath), so the built test binary needs no LD_LIBRARY_PATH.
- Toolchain
- Go 1.21+ (go.mod says 1.21) with CGO_ENABLED=1 and a C compiler
- Distribution
- No registry package: go get the go/v2 module at the release tag, plus the release native library
- Finding the library
- Link time: cgo's -lservirtium_vcr must find a file literally named libservirtium_vcr.so / .dylib. Point CGO_LDFLAGS at the directory (-L and -Wl,-rpath). SERVIRTIUM_VCR_LIB is for the repo's own tests, not for consumers.
- On GitHub
- go/ · README
Getting it
There is no published package for Go. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.
- Add the Go module. It lives in the go/ subdirectory of servirtium-vcr with the major version in the path (/v2); each release is also tagged go/<tag>, which is what go get reads.
go get github.com/servirtium/servirtium-vcr/go/v2@v2.0.0-alpha.20 - Fetch the native library for your OS/arch from the release into ./native, verify the checksum, and copy it to the plain name cgo links against.
REL=https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20 LIB=libservirtium_vcr-v2.0.0-alpha.20-macos-arm64.dylib # or -linux-x86_64.so, -linux-arm64.so, ... mkdir -p native curl -fsSL -o native/$LIB $REL/$LIB curl -fsSL -o native/$LIB.sha256 $REL/$LIB.sha256 ( cd native && { sha256sum -c $LIB.sha256 || shasum -a 256 -c $LIB.sha256; } ) cp native/$LIB native/libservirtium_vcr.dylib # .so on Linux/FreeBSD - Tell cgo where the library is, in every shell and CI step that runs go build / go test.
export CGO_LDFLAGS="-L$PWD/native -Wl,-rpath,$PWD/native" go test ./... - macOS only, for v2.0.0-alpha.20: the released .dylib carries a CI build path as its install name, so a linked Go binary dies at start with 'Library not loaded: /home/runner/...'. Rewrite the install name once, after the copy (ad-hoc re-sign included).
install_name_tool -id @rpath/libservirtium_vcr.dylib native/libservirtium_vcr.dylib codesign -f -s - native/libservirtium_vcr.dylib
Record
Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.
package mypkg_test
import (
"net/http"
"testing"
servirtium "github.com/servirtium/servirtium-vcr/go/v2"
)
func TestRecordClimate(t *testing.T) {
srv, err := servirtium.Record("tapes/climate_api.md", "https://climatedataapi.worldbank.org").
Port(0). // 0 = OS-assigned
Start()
if err != nil {
t.Fatal(err)
}
defer srv.Discard() // no-op after Commit; on a failure path it drops the recording
resp, err := http.Get(srv.BaseURL() + "/api/v1/countries")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if err := srv.Commit(); err != nil { // the only thing that writes the tape
t.Fatal(err)
}
}
Playback
The same tape, replayed with no network.
func TestPlaybackClimate(t *testing.T) {
srv, err := servirtium.Playback("tapes/climate_api.md").Port(0).Start()
if err != nil {
t.Fatal(err)
}
defer srv.Close()
resp, err := http.Get(srv.BaseURL() + "/api/v1/countries")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if srv.LastKind() != servirtium.Ok { // optional: assert a clean match
t.Fatal(srv.LastError())
}
}
Idiomatic extras
Discard on the failure path
defer Discard() right after Start(): after a Commit it does nothing, but if the test fails first the half-finished recording is dropped and no tape is written.
srv, _ := servirtium.Record(tape, upstream).Port(0).Start()
defer srv.Discard()
// ... drive the SUT; t.Fatal here leaves no tape behind ...
if err := srv.Commit(); err != nil {
t.Fatal(err)
}
Redact credentials and JSON fields at record time
Redactions apply when the tape is written, so the live test still sees real bytes while the committed tape is clean.
servirtium.Record(tape, upstream).
RedactCredentials().
RedactJSONFieldWith("api_key", "[REDACTED]", servirtium.ResponseBody).
Start()
Drift detection
With FailIfChanged, Commit still writes the fresh tape but returns an error if it differs from the one on disk, so git diff shows the drift.
srv, _ := servirtium.Record(tape, upstream).FailIfChanged().Start()
// ... drive the SUT ...
if err := srv.Commit(); err != nil {
t.Fatal(err)
}
Gotchas
- cgo is required: CGO_ENABLED=0 fails with 'build constraints exclude all Go files'. A C compiler must be on PATH.
- The import path must end in /go/v2; .../servirtium-vcr/go without /v2 is wrong.
- go mod vendor copies Go files only; the native library does not follow. Do not write into GOMODCACHE.
- Go's http.Client sends a default User-Agent; under StrictHeaders() that header must be on the recorded request block or be suppressed.
- v2.0.0-alpha.20 macOS dylib has a bogus install name (a CI path); see the install step.
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your Go project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.
Hey Gemini or Claude or Codex,
I want a test in this Go project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.
1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.
2. Add the Servirtium binding for Go. Follow https://servirtium.dev/languages/go/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: No registry package: go get the go/v2 module at the release tag, plus the release native library.
Toolchain and minimum version: Go 1.21+ (go.mod says 1.21) with CGO_ENABLED=1 and a C compiler.
How the binding finds the native library: Link time: cgo's -lservirtium_vcr must find a file literally named libservirtium_vcr.so / .dylib. Point CGO_LDFLAGS at the directory (-L and -Wl,-rpath). SERVIRTIUM_VCR_LIB is for the repo's own tests, not for consumers.
3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Go way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.
4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for Go is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.
5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.
6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.
7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.
Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- cgo is required: CGO_ENABLED=0 fails with 'build constraints exclude all Go files'. A C compiler must be on PATH.
- The import path must end in /go/v2; .../servirtium-vcr/go without /v2 is wrong.
- go mod vendor copies Go files only; the native library does not follow. Do not write into GOMODCACHE.
- Go's http.Client sends a default User-Agent; under StrictHeaders() that header must be on the recorded request block or be suppressed.
- v2.0.0-alpha.20 macOS dylib has a bogus install name (a CI path); see the install step.
References:
- This language's page: https://servirtium.dev/languages/go/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/go
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)
How these snippets were checked
Real run on macOS arm64 (Go 1.27.4): go get the go/v2 module at v2.0.0-alpha.20, release macos-arm64 dylib (sha256 verified) copied to native/libservirtium_vcr.dylib, CGO_LDFLAGS as above. A go test passed three cases: playback of single_get.md (body ok-body, LastKind Ok), record against an httptest upstream then Commit then replay of the new tape, and Record then Discard (no tape file). The UNMODIFIED release dylib fails at test start with dyld 'Library not loaded: /home/runner/work/.../libservirtium_vcr.so' (install name); the run passed only after install_name_tool -id @rpath/... + codesign. Redact/FailIfChanged extras and the deferred-Discard-after-Commit idiom were checked against servirtium.go (Discard returns when the handle is already nil) but not executed.
They mirror these files in the repository:
See also the feature reference, llms.txt and the go/ directory.