CodeSampleX

Exemple

github.com/go-stack/stack v1.6.0: capture and format call stack and caller frames with stack.Caller and stack.Trace

Échantillon vérifié pour golang github.com/go-stack/stack v1.6.0: capture and format call stack and caller frames with stack.Caller and stack.Trace. Le…

sha256:7640c0d86d3416ac7a739a9ed70c306ce7cf8dceae70c7487e6afc887e55c88b

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré go 1.26 linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1.26 go 1

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
capture and format call stack and caller frames with stack.Caller and stack.Trace
Paquets
Symboles
  • stack.Caller
  • stack.Trace
Environnement
go 1.26.6
Créé
2026-08-28T09:41:17Z

Contrat

  1. stack.Caller returns a Call record for a stack frame with valid PC and function name
  2. stack.Trace returns a CallStack capturing the sequence of function invocations
  3. stack.Call formats function name, source file and line number with fmt verbs %n, %s, %d
  4. stack.CallStack.TrimBelow and TrimAbove truncate stack frames relative to a Call record

Fichiers

  • PROMPT.md
  • contract_test.go
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json

Télécharger l’artefact source (tar.gz)

Code source

PROMPT.md
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.
contract_test.go
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"))
	}
}
csx.json
{"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"}
go.mod
module example.com/sample

go 1.26.6

require github.com/go-stack/stack v1.6.0
go.sum
github.com/go-stack/stack v1.6.0 h1:MmJCxYVKTJ0SplGKqFVX3SBnmaUhODHZrrFF6jMbpZk=
github.com/go-stack/stack v1.6.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
sample.go
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)
}
spec.json
{
  "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"
  ]
}

Seeder d'origine

anonyme