示例
github.com/google/uuid v1.6.0: binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary
已验证示例 — golang github.com/google/uuid v1.6.0: binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary. contract 在 go 1.26 · linux…
sha256:8525be64586c339bd0d5b7d875cc90b3509b1e13e99976deb399027730db87ce
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 2
- 构建过它的签名密钥
- 2
声明的环境
windows 11 x64 go
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| go 1.26 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1 |
2026-08-19 |
| 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-09-07 |
案例
HOW- 目标
- binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary
- 符号
-
- github.com/google/uuid.UUID.MarshalBinary
- 创建时间
- 2026-08-19T04:21:00Z
契约
- UUID.MarshalBinary encodes UUID into a 16-byte binary representation without error
- UUID.UnmarshalBinary reconstructs exact original UUID from valid 16-byte slice
- UUID.UnmarshalBinary returns error for byte slices of invalid length
- UUID.MarshalBinary and UnmarshalBinary support uuid.Nil and arbitrary UUIDs in lossless round trip
文件
- PROMPT.md
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
- test/contract.go
源代码
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: binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/github.com/google/uuid@v1.6.0
Demonstrate these symbols/APIs:
- github.com/google/uuid.UUID.MarshalBinary
Constraints:
- executionContext: node
Required runtime conditions:
- ecosystem: npm
- language: javascript
- moduleSystem: cjs
- packageManager: npm@11.8.0
- runtime: node@24.13.1
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.
{"case":{"caseId":"case:sha256:e1d764c3879c227351c0c439d0824016c32ef3c68db4782796e11ab2868c014e","contract":["UUID.MarshalBinary encodes UUID into a 16-byte binary representation without error","UUID.UnmarshalBinary reconstructs exact original UUID from valid 16-byte slice","UUID.UnmarshalBinary returns error for byte slices of invalid length","UUID.MarshalBinary and UnmarshalBinary support uuid.Nil and arbitrary UUIDs in lossless round trip"],"goal":"binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary","kind":"HOW","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["github.com/google/uuid.UUID.MarshalBinary"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","ecosystem":"golang","os":"windows","osVersionBucket":"11","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["github.com/google/uuid.UUID.MarshalBinary"],"verifierAdapter":"golang@1"}
module sample
go 1.22.0
require github.com/google/uuid v1.6.0
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
package sample
import (
"github.com/google/uuid"
)
// EncodeUUID serializes a UUID into its 16-byte binary representation.
func EncodeUUID(u uuid.UUID) ([]byte, error) {
return u.MarshalBinary()
}
// DecodeUUID deserializes a 16-byte binary slice into a UUID.
func DecodeUUID(data []byte) (uuid.UUID, error) {
var u uuid.UUID
err := u.UnmarshalBinary(data)
if err != nil {
return uuid.Nil, err
}
return u, nil
}
{
"schemaVersion": 1,
"goal": "binary serialization with github.com/google/uuid MarshalBinary and UnmarshalBinary",
"kind": "HOW",
"packages": [
"pkg:golang/github.com/google/uuid@v1.6.0"
],
"symbols": [
"github.com/google/uuid.UUID.MarshalBinary"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@11.8.0",
"runtime": "node@24.13.1"
}
}
package main
import (
"bytes"
"fmt"
"os"
"github.com/google/uuid"
)
func main() {
// 1. MarshalBinary encodes a UUID into a 16-byte slice without error
original := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
data, err := original.MarshalBinary()
if err != nil {
fmt.Fprintf(os.Stderr, "unexpected error in MarshalBinary: %v\n", err)
os.Exit(1)
}
if len(data) != 16 {
fmt.Fprintf(os.Stderr, "expected binary length 16, got %d\n", len(data))
os.Exit(1)
}
// Verify byte content matches raw UUID bytes
if !bytes.Equal(data, original[:]) {
fmt.Fprintf(os.Stderr, "MarshalBinary output does not match raw bytes\n")
os.Exit(1)
}
// 2. UnmarshalBinary reconstructs the exact UUID from valid 16-byte slice
var decoded uuid.UUID
if err := decoded.UnmarshalBinary(data); err != nil {
fmt.Fprintf(os.Stderr, "unexpected error in UnmarshalBinary: %v\n", err)
os.Exit(1)
}
if decoded != original {
fmt.Fprintf(os.Stderr, "expected decoded UUID %v, got %v\n", original, decoded)
os.Exit(1)
}
// 3. UnmarshalBinary returns error for byte slices not exactly 16 bytes
invalidSlices := [][]byte{
{},
make([]byte, 15),
make([]byte, 17),
make([]byte, 32),
}
for _, invalid := range invalidSlices {
var u uuid.UUID
if err := u.UnmarshalBinary(invalid); err == nil {
fmt.Fprintf(os.Stderr, "expected error for invalid byte slice length %d, got nil\n", len(invalid))
os.Exit(1)
}
}
// 4. MarshalBinary and UnmarshalBinary work correctly for uuid.Nil
nilData, err := uuid.Nil.MarshalBinary()
if err != nil {
fmt.Fprintf(os.Stderr, "unexpected error in uuid.Nil.MarshalBinary: %v\n", err)
os.Exit(1)
}
if !bytes.Equal(nilData, make([]byte, 16)) {
fmt.Fprintf(os.Stderr, "expected 16 zero bytes for nil UUID, got %v\n", nilData)
os.Exit(1)
}
var unmarshaledNil uuid.UUID
if err := unmarshaledNil.UnmarshalBinary(nilData); err != nil {
fmt.Fprintf(os.Stderr, "unexpected error unmarshaling nil UUID: %v\n", err)
os.Exit(1)
}
if unmarshaledNil != uuid.Nil {
fmt.Fprintf(os.Stderr, "expected uuid.Nil, got %v\n", unmarshaledNil)
os.Exit(1)
}
// 5. Random UUID round-trip test
randomUUID := uuid.New()
randomBytes, err := randomUUID.MarshalBinary()
if err != nil {
fmt.Fprintf(os.Stderr, "unexpected error marshaling random UUID: %v\n", err)
os.Exit(1)
}
var roundTripUUID uuid.UUID
if err := roundTripUUID.UnmarshalBinary(randomBytes); err != nil {
fmt.Fprintf(os.Stderr, "unexpected error unmarshaling random UUID: %v\n", err)
os.Exit(1)
}
if roundTripUUID != randomUUID {
fmt.Fprintf(os.Stderr, "round-trip mismatch: got %v, want %v\n", roundTripUUID, randomUUID)
os.Exit(1)
}
fmt.Println("All contract assertions passed.")
}
原始种子者
匿名