Beispiel
github.com/go-stack/stack v1.6.0: capture and format call stack and caller frames with stack.Caller and stack.Trace
Verifiziertes Beispiel für golang github.com/go-stack/stack v1.6.0: capture and format call stack and caller frames with stack.Caller and stack.Trace. Der…
sha256:7640c0d86d3416ac7a739a9ed70c306ce7cf8dceae70c7487e6afc887e55c88b
Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen.
Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen.
MIT-0
Ausführungsbelege
Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.
- Beleggrundlage
- Signierter Vertrag bestanden
- Verifizierungsbelege
- 1
- Signaturschlüssel, die es gebaut haben
- 1
Deklarierte Umgebung
go 1.26 linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1.26 go 1
Umgebungen der Verifizierungsläufe
| Umgebung | Contract | Stufen | Lauf |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f… |
2026-08-28 |
Fall
HOW- Ziel
- capture and format call stack and caller frames with stack.Caller and stack.Trace
- Symbole
-
- stack.Caller
- stack.Trace
- Umgebung
- go 1.26.6
- Erstellt
- 2026-08-28T09:41:17Z
Contract
- stack.Caller returns a Call record for a stack frame with valid PC and function name
- stack.Trace returns a CallStack capturing the sequence of function invocations
- stack.Call formats function name, source file and line number with fmt verbs %n, %s, %d
- stack.CallStack.TrimBelow and TrimAbove truncate stack frames relative to a Call record
Dateien
- PROMPT.md
- contract_test.go
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
Quelltext
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: capture and format call stack and caller frames with stack.Caller and stack.Trace
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/github.com/go-stack/stack@v1.6.0
Demonstrate these symbols/APIs:
- stack.Caller
- stack.Trace
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
package sample
import (
"strings"
"testing"
"github.com/go-stack/stack"
)
func levelA() stack.Call {
return GetCaller(0)
}
func levelB() (stack.CallStack, stack.Call) {
trace := GetTrace()
caller := GetCaller(0)
return trace, caller
}
func TestContractCaller(t *testing.T) {
call := levelA()
// 1. Verify PC is non-zero
if call.PC() == 0 {
t.Fatalf("expected non-zero PC for caller")
}
// 2. Verify function name formatting (%n)
funcName := FormatCall(call, "%n")
if funcName != "levelA" {
t.Fatalf("expected function name 'levelA', got %q", funcName)
}
// 3. Verify source file formatting (%s)
sourceFile := FormatCall(call, "%s")
if !strings.HasSuffix(sourceFile, "contract_test.go") {
t.Fatalf("expected source file ending with contract_test.go, got %q", sourceFile)
}
// 4. Verify line number formatting (%d)
lineStr := FormatCall(call, "%d")
if lineStr == "" || lineStr == "0" {
t.Fatalf("expected valid line number, got %q", lineStr)
}
// 5. Verify string formatting (%v)
str := call.String()
if !strings.Contains(str, "contract_test.go:") {
t.Fatalf("expected String() to contain 'contract_test.go:', got %q", str)
}
}
func TestContractTrace(t *testing.T) {
trace, _ := levelB()
// 1. Verify trace contains frames
if len(trace) < 2 {
t.Fatalf("expected at least 2 frames in trace, got %d", len(trace))
}
// 2. Verify top frame is levelB or GetTrace
topName := FormatCall(trace[0], "%n")
if topName != "levelB" && topName != "GetTrace" {
t.Fatalf("expected top frame to be levelB or GetTrace, got %q", topName)
}
// 3. Format entire CallStack with FormatTrace
traceStr := FormatTrace(trace, "%n")
if !strings.HasPrefix(traceStr, "[") || !strings.HasSuffix(traceStr, "]") {
t.Fatalf("expected formatted CallStack in square brackets, got %q", traceStr)
}
// 4. Test TrimBelow and TrimAbove with a frame from trace
targetFrame := trace[1]
targetName := FormatCall(targetFrame, "%n")
trimmedBelow := TrimBelow(trace, targetFrame)
if len(trimmedBelow) == 0 {
t.Fatalf("expected non-empty stack after TrimBelow")
}
if FormatCall(trimmedBelow[0], "%n") != targetName {
t.Fatalf("expected first frame of trimmedBelow to be %q, got %q", targetName, FormatCall(trimmedBelow[0], "%n"))
}
trimmedAbove := TrimAbove(trace, targetFrame)
if len(trimmedAbove) == 0 {
t.Fatalf("expected non-empty stack after TrimAbove")
}
lastIdx := len(trimmedAbove) - 1
if FormatCall(trimmedAbove[lastIdx], "%n") != targetName {
t.Fatalf("expected last frame of trimmedAbove to be %q, got %q", targetName, FormatCall(trimmedAbove[lastIdx], "%n"))
}
}
{"case":{"caseId":"case:sha256:d363e4f0d198b097345db5b4c5803691efed77ce737c569162bb778c0f2d580f","contract":["stack.Caller returns a Call record for a stack frame with valid PC and function name","stack.Trace returns a CallStack capturing the sequence of function invocations","stack.Call formats function name, source file and line number with fmt verbs %n, %s, %d","stack.CallStack.TrimBelow and TrimAbove truncate stack frames relative to a Call record"],"goal":"capture and format call stack and caller frames with stack.Caller and stack.Trace","kind":"HOW","packages":["pkg:golang/github.com/go-stack/stack@v1.6.0"],"schemaVersion":1,"symbols":["stack.Caller","stack.Trace"]},"contractCommand":["go","test","./..."],"environment":{"arch":"amd64","compiler":"go","compilerVersion":"1.26.6","distro":"ubuntu","ecosystem":"golang","language":"go","languageVersion":"1.26.6","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/go-stack/stack@v1.6.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/go-stack/stack@v1.6.0","symbols":["stack.Caller","stack.Trace"],"verifierAdapter":"golang@1"}
module example.com/sample
go 1.26.6
require github.com/go-stack/stack v1.6.0
github.com/go-stack/stack v1.6.0 h1:MmJCxYVKTJ0SplGKqFVX3SBnmaUhODHZrrFF6jMbpZk=
github.com/go-stack/stack v1.6.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
package sample
import (
"fmt"
"github.com/go-stack/stack"
)
// GetCaller returns the Call for the function at skip levels above the caller of GetCaller.
// skip=0 returns the immediate caller of GetCaller.
func GetCaller(skip int) stack.Call {
return stack.Caller(skip + 1)
}
// GetTrace returns the CallStack for the current goroutine.
func GetTrace() stack.CallStack {
return stack.Trace()
}
// FormatCall formats a Call using the specified fmt verb/flags (e.g. "%n", "%s", "%d", "%+v").
func FormatCall(c stack.Call, verb string) string {
return fmt.Sprintf(verb, c)
}
// FormatTrace formats a CallStack using the specified fmt verb/flags.
func FormatTrace(cs stack.CallStack, verb string) string {
return fmt.Sprintf(verb, cs)
}
// TrimBelow trims entries below c (frames deeper than c).
func TrimBelow(cs stack.CallStack, c stack.Call) stack.CallStack {
return cs.TrimBelow(c)
}
// TrimAbove trims entries above c (frames higher than c).
func TrimAbove(cs stack.CallStack, c stack.Call) stack.CallStack {
return cs.TrimAbove(c)
}
{
"schemaVersion": 1,
"goal": "capture and format call stack and caller frames with stack.Caller and stack.Trace",
"kind": "HOW",
"packages": [
"pkg:golang/github.com/go-stack/stack@v1.6.0"
],
"symbols": [
"stack.Caller",
"stack.Trace"
]
}
Ursprungs-Seeder
anonym