CodeSampleX

Beispiel

golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543: As

Verifiziertes Beispiel für golang golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543: As. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker und…

sha256:dc94975ebaea6e26c0ad9f2d3caa445ec52eb19cf64fd491bac94d8e4c4fa66a

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 x64 go 1.26 go go 1

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-05

Fall

HOW
Ziel
verify xerrors.As in pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543
Pakete
Symbole
  • xerrors.As
Umgebung
go 1.26.6
Erstellt
2026-09-05T05:41:38Z

Contract

  1. xerrors.As finds and extracts a matching pointer to struct in an error chain
  2. xerrors.As returns false and leaves target unchanged when type does not match
  3. xerrors.As finds and extracts an error implementing a target interface
  4. xerrors.As supports custom As method on error types
  5. xerrors.As panics when target is nil or not a pointer to error or interface

Dateien

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Quellartefakt herunterladen (tar.gz)

Quelltext

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: verify xerrors.As in pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543
Demonstrate these symbols/APIs:
  - xerrors.As
Required runtime conditions:
  - ecosystem: golang
  - language: go
  - packageManager: go@1.26.6
  - runtime: go@1.26.6

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.
csx.json
{"case":{"caseId":"case:sha256:3ae202748a0bb0f63b724d5a6d17919ab176ad03d33dfb8086d0bbb4622b4587","contract":["xerrors.As finds and extracts a matching pointer to struct in an error chain","xerrors.As returns false and leaves target unchanged when type does not match","xerrors.As finds and extracts an error implementing a target interface","xerrors.As supports custom As method on error types","xerrors.As panics when target is nil or not a pointer to error or interface"],"goal":"verify xerrors.As in pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543","kind":"HOW","packages":["pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543"],"schemaVersion":1,"symbols":["xerrors.As"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","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/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543","symbols":["xerrors.As"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22.0

require golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
go.sum
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
main.go
package main

import (
	"fmt"
	"os"

	"golang.org/x/xerrors"
)

// ItemNotFoundError indicates that a requested item was not found.
type ItemNotFoundError struct {
	Resource string
	ID       string
}

func (e *ItemNotFoundError) Error() string {
	return fmt.Sprintf("%s with ID %q was not found", e.Resource, e.ID)
}

// ResourceIdentifier allows querying the resource name of an error.
type ResourceIdentifier interface {
	Error() string
}

func findItem(id string) error {
	return &ItemNotFoundError{
		Resource: "document",
		ID:       id,
	}
}

func processItem(id string) error {
	if err := findItem(id); err != nil {
		return xerrors.Errorf("processing item %s failed: %w", id, err)
	}
	return nil
}

func main() {
	err := processItem("doc-42")
	if err == nil {
		fmt.Println("success")
		return
	}

	fmt.Printf("Encountered error: %v\n", err)

	// Use xerrors.As to inspect and extract the typed ItemNotFoundError
	var notFoundErr *ItemNotFoundError
	if xerrors.As(err, &notFoundErr) {
		fmt.Printf("Handled ItemNotFoundError: resource=%s id=%s\n", notFoundErr.Resource, notFoundErr.ID)
	} else {
		fmt.Fprintf(os.Stderr, "Expected ItemNotFoundError in error chain\n")
		os.Exit(1)
	}

	// Use xerrors.As to check interface implementation
	var resErr ResourceIdentifier
	if xerrors.As(err, &resErr) {
		fmt.Printf("Handled ResourceIdentifier interface: %s\n", resErr.Error())
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify xerrors.As in pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543"
  ],
  "symbols": [
    "xerrors.As"
  ],
  "runtimeConditions": {
    "ecosystem": "golang",
    "language": "go",
    "packageManager": "go@1.26.6",
    "runtime": "go@1.26.6"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"golang.org/x/xerrors"
)

type appError struct {
	code   int
	detail string
}

func (e *appError) Error() string {
	return fmt.Sprintf("app error %d: %s", e.code, e.detail)
}

func (e *appError) StatusCode() int {
	return e.code
}

type otherError struct {
	reason string
}

func (e *otherError) Error() string {
	return fmt.Sprintf("other error: %s", e.reason)
}

type statusCoder interface {
	StatusCode() int
}

type customErrorTarget struct {
	resolved int
}

func (e *customErrorTarget) Error() string {
	return fmt.Sprintf("custom error target code %d", e.resolved)
}

type customAsError struct {
	customCode int
}

func (e *customAsError) Error() string {
	return fmt.Sprintf("custom as error code %d", e.customCode)
}

func (e *customAsError) As(target interface{}) bool {
	if p, ok := target.(**customErrorTarget); ok {
		*p = &customErrorTarget{resolved: e.customCode}
		return true
	}
	return false
}

func assertPanic(name string, f func()) {
	defer func() {
		if r := recover(); r == nil {
			fmt.Fprintf(os.Stderr, "FAIL: %s expected panic, got none\n", name)
			os.Exit(1)
		}
	}()
	f()
}

func main() {
	// Assertion 1: xerrors.As finds and extracts a matching pointer to struct in an error chain
	origErr := &appError{code: 404, detail: "resource missing"}
	chain := xerrors.Errorf("outer wrap: %w", xerrors.Errorf("inner wrap: %w", origErr))

	var matchedAppErr *appError
	if !xerrors.As(chain, &matchedAppErr) {
		fmt.Fprintf(os.Stderr, "FAIL: xerrors.As failed to match *appError in chain\n")
		os.Exit(1)
	}
	if matchedAppErr != origErr {
		fmt.Fprintf(os.Stderr, "FAIL: target pointer mismatch: got %p, want %p\n", matchedAppErr, origErr)
		os.Exit(1)
	}
	if matchedAppErr.code != 404 {
		fmt.Fprintf(os.Stderr, "FAIL: unexpected code: got %d, want 404\n", matchedAppErr.code)
		os.Exit(1)
	}

	// Assertion 2: xerrors.As returns false and leaves target unchanged when type does not match
	var mismatch *otherError
	if xerrors.As(chain, &mismatch) {
		fmt.Fprintf(os.Stderr, "FAIL: xerrors.As unexpectedly matched *otherError\n")
		os.Exit(1)
	}
	if mismatch != nil {
		fmt.Fprintf(os.Stderr, "FAIL: mismatch target should remain nil, got %v\n", mismatch)
		os.Exit(1)
	}

	// Assertion 3: xerrors.As finds and extracts an error implementing a target interface
	var sc statusCoder
	if !xerrors.As(chain, &sc) {
		fmt.Fprintf(os.Stderr, "FAIL: xerrors.As failed to match statusCoder interface\n")
		os.Exit(1)
	}
	if sc == nil || sc.StatusCode() != 404 {
		fmt.Fprintf(os.Stderr, "FAIL: statusCoder returned invalid status: %v\n", sc)
		os.Exit(1)
	}

	// Assertion 4: xerrors.As supports custom As method on error types
	customErr := &customAsError{customCode: 99}
	customChain := xerrors.Errorf("wrap: %w", customErr)
	var customTarget *customErrorTarget
	if !xerrors.As(customChain, &customTarget) {
		fmt.Fprintf(os.Stderr, "FAIL: xerrors.As failed on custom As implementation\n")
		os.Exit(1)
	}
	if customTarget == nil || customTarget.resolved != 99 {
		fmt.Fprintf(os.Stderr, "FAIL: custom target mismatch: got %v\n", customTarget)
		os.Exit(1)
	}

	// Assertion 5: xerrors.As panics when target is nil or not a pointer to error or interface
	assertPanic("nil target", func() {
		xerrors.As(chain, nil)
	})
	assertPanic("non-pointer target", func() {
		xerrors.As(chain, appError{})
	})

	fmt.Println("PASS: pkg:golang/golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543 contract passed")
}

Ursprungs-Seeder

anonym