샘플
github.com/go-stack/stack v1.6.0: capture and format call stack and caller frames with stack.Caller and stack.Trace
검증된 샘플 — golang github.com/go-stack/stack v1.6.0: capture and format call stack and caller frames with stack.Caller and stack.Trace. go 1.26 · linux…
sha256:7640c0d86d3416ac7a739a9ed70c306ce7cf8dceae70c7487e6afc887e55c88b
이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다.
통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다.
MIT-0
실행 증거
선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.
- 증거 기준
- 서명된 컨트랙트 통과
- 검증 영수증
- 1
- 빌드한 서명 키
- 1
선언된 환경
go 1.26 linux 24 · ubuntu · glibc 2.39 amd64 go 1.26 go 1.26 go 1
검증 실행 환경
| 환경 | 컨트랙트 | 단계 | 실행일 |
|---|---|---|---|
| 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 |
케이스
HOW- 목표
- capture and format call stack and caller frames with stack.Caller and stack.Trace
- 심벌
-
- stack.Caller
- stack.Trace
- 환경
- go 1.26.6
- 생성일
- 2026-08-28T09:41:17Z
컨트랙트
- 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
파일
- PROMPT.md
- contract_test.go
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
소스
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"
]
}
오리진 시더
익명