CodeSampleX

示例

github.com/google/go-cmp v0.6.0

已验证示例 — golang github.com/google/go-cmp v0.6.0. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: assert cmp.Equal returns true when comparing identical…

sha256:e04c363e45ee265834a86a4cd2db305937ce88a0728272c287466718e7bc9209

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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-13

案例

HOW
目标
verify pkg:golang/github.com/google/go-cmp@v0.6.0
包
创建时间
2026-09-13T21:01:40Z

契约

  1. assert cmp.Equal returns true when comparing identical basic types and struct values
  2. assert cmp.Diff returns empty string for equal values and non-empty diff string describing field differences
  3. assert cmp.Diff panics on unexported fields by default unless cmp.AllowUnexported is provided
  4. assert cmpopts.IgnoreFields excludes specified struct fields from comparison
  5. assert cmp.Comparer customizes equality evaluation between types

文件

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

下载源代码构件 (tar.gz)

源代码

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 pkg:golang/github.com/google/go-cmp@v0.6.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/go-cmp@v0.6.0

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:fe1c52d4051d8322eb291792f2449e793995aef8a503e6a3a4956d2eaa317b59","contract":["assert cmp.Equal returns true when comparing identical basic types and struct values","assert cmp.Diff returns empty string for equal values and non-empty diff string describing field differences","assert cmp.Diff panics on unexported fields by default unless cmp.AllowUnexported is provided","assert cmpopts.IgnoreFields excludes specified struct fields from comparison","assert cmp.Comparer customizes equality evaluation between types"],"goal":"verify pkg:golang/github.com/google/go-cmp@v0.6.0","kind":"HOW","packages":["pkg:golang/github.com/google/go-cmp@v0.6.0"],"schemaVersion":1},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/go-cmp@v0.6.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/google/go-cmp@v0.6.0","verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/google/go-cmp v0.6.0
go.sum
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
main.go
package main

import (
	"fmt"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

// User represents a user record with both public and private fields.
type User struct {
	ID    int
	Name  string
	Role  string
	token string
}

func main() {
	u1 := User{ID: 1, Name: "Alice", Role: "Admin", token: "secret-token-1"}
	u2 := User{ID: 1, Name: "Alice", Role: "Admin", token: "secret-token-2"}

	// Compare with AllowUnexported and IgnoreFields
	diff := cmp.Diff(u1, u2, cmp.AllowUnexported(User{}), cmpopts.IgnoreFields(User{}, "token"))
	if diff == "" {
		fmt.Println("Users match when ignoring token field")
	} else {
		fmt.Printf("Users differ:\n%s\n", diff)
	}

	u3 := User{ID: 2, Name: "Bob", Role: "Member", token: "secret-token-1"}
	diff2 := cmp.Diff(u1, u3, cmp.AllowUnexported(User{}))
	fmt.Printf("Difference between Alice and Bob:\n%s\n", diff2)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/google/go-cmp@v0.6.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/go-cmp@v0.6.0"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

type Item struct {
	ID        int
	Title     string
	Timestamp int64
}

type Record struct {
	ID     int
	secret string
}

func main() {
	// Assertion 1: cmp.Equal returns true when comparing identical basic types and struct values
	{
		i1 := Item{ID: 10, Title: "first", Timestamp: 100}
		i2 := Item{ID: 10, Title: "first", Timestamp: 100}
		if !cmp.Equal(i1, i2) {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: identical items should be equal\n")
			os.Exit(1)
		}
		if !cmp.Equal([]string{"a", "b"}, []string{"a", "b"}) {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: identical slices should be equal\n")
			os.Exit(1)
		}
	}

	// Assertion 2: cmp.Diff returns empty string for equal values and non-empty diff string describing field differences
	{
		i1 := Item{ID: 10, Title: "alpha", Timestamp: 100}
		i2 := Item{ID: 10, Title: "beta", Timestamp: 100}
		diff := cmp.Diff(i1, i2)
		if diff == "" || !strings.Contains(diff, "alpha") || !strings.Contains(diff, "beta") {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: diff should describe field mismatch, got %q\n", diff)
			os.Exit(1)
		}

		sameDiff := cmp.Diff(i1, i1)
		if sameDiff != "" {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: equal values must produce empty diff\n")
			os.Exit(1)
		}
	}

	// Assertion 3: cmp.Diff panics on unexported fields by default unless cmp.AllowUnexported is provided
	{
		r1 := Record{ID: 1, secret: "a"}
		r2 := Record{ID: 1, secret: "b"}

		panicked := false
		func() {
			defer func() {
				if r := recover(); r != nil {
					panicked = true
				}
			}()
			_ = cmp.Diff(r1, r2)
		}()

		if !panicked {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: cmp.Diff must panic on unexported fields without AllowUnexported\n")
			os.Exit(1)
		}

		diffAllowed := cmp.Diff(r1, r2, cmp.AllowUnexported(Record{}))
		if diffAllowed == "" || !strings.Contains(diffAllowed, "secret") {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: cmp.Diff with AllowUnexported should report secret difference, got %q\n", diffAllowed)
			os.Exit(1)
		}
	}

	// Assertion 4: cmpopts.IgnoreFields excludes specified struct fields from comparison
	{
		i1 := Item{ID: 100, Title: "sample", Timestamp: 1000}
		i2 := Item{ID: 100, Title: "sample", Timestamp: 9999}

		// Without ignore, they are different
		if cmp.Equal(i1, i2) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: timestamps differ, should not be equal without IgnoreFields\n")
			os.Exit(1)
		}

		// With ignore, they evaluate as equal
		if !cmp.Equal(i1, i2, cmpopts.IgnoreFields(Item{}, "Timestamp")) {
			fmt.Fprintf(os.Stderr, "assertion 4 failed: IgnoreFields should cause items to evaluate equal\n")
			os.Exit(1)
		}
	}

	// Assertion 5: cmp.Comparer customizes equality evaluation between types
	{
		type ApproxFloat float64
		approxEqual := cmp.Comparer(func(x, y ApproxFloat) bool {
			diff := float64(x - y)
			if diff < 0 {
				diff = -diff
			}
			return diff < 0.01
		})

		v1 := ApproxFloat(1.000)
		v2 := ApproxFloat(1.005)
		v3 := ApproxFloat(1.050)

		if !cmp.Equal(v1, v2, approxEqual) {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: approxEqual should consider 1.000 and 1.005 equal\n")
			os.Exit(1)
		}
		if cmp.Equal(v1, v3, approxEqual) {
			fmt.Fprintf(os.Stderr, "assertion 5 failed: approxEqual should consider 1.000 and 1.050 unequal\n")
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed successfully.")
}

原始种子者

匿名