CodeSampleX

示例

github.com/google/uuid v1.6.0: New, Parse, ParseBytes

已验证示例 — golang github.com/google/uuid v1.6.0: New, Parse, ParseBytes. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: uuid.New generates a valid…

sha256:a38eeefc186090293c63986424076ffc26161543146a554351045ec5858c6500

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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
目标
verify pkg:golang/github.com/google/uuid@v1.6.0
包
符号
  • New
  • Parse
  • ParseBytes
  • Validate
  • FromBytes
创建时间
2026-08-19T09:32:05Z

契约

  1. uuid.New generates a valid version 4 UUID with RFC4122 variant
  2. uuid.Parse and uuid.ParseBytes parse 36-character hyphenated UUID strings and byte slices
  3. uuid.Parse parses 32-character hexadecimal UUID format and URN formatted strings
  4. uuid.Parse and uuid.Validate return errors for malformed or invalid length UUID strings
  5. uuid.FromBytes constructs UUID from 16-byte slice and rejects invalid slice lengths
  6. uuid.Nil represents zero value UUID with Version 0 and Reserved variant

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.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/uuid@v1.6.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/uuid@v1.6.0
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.
csx.json
{"case":{"caseId":"case:sha256:b56bf2f5d836124c9de874a9244b3d786aa0e356ebc6675e71f4fc36bfeaf5fa","contract":["uuid.New generates a valid version 4 UUID with RFC4122 variant","uuid.Parse and uuid.ParseBytes parse 36-character hyphenated UUID strings and byte slices","uuid.Parse parses 32-character hexadecimal UUID format and URN formatted strings","uuid.Parse and uuid.Validate return errors for malformed or invalid length UUID strings","uuid.FromBytes constructs UUID from 16-byte slice and rejects invalid slice lengths","uuid.Nil represents zero value UUID with Version 0 and Reserved variant"],"goal":"verify pkg:golang/github.com/google/uuid@v1.6.0","kind":"HOW","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["New","Parse","ParseBytes","Validate","FromBytes"]},"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":["New","Parse","ParseBytes","Validate","FromBytes"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.22.0

require github.com/google/uuid v1.6.0
go.sum
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=
sample.go
package sample

import (
	"github.com/google/uuid"
)

// GenerateUUID generates a new RFC 4122 Version 4 UUID.
func GenerateUUID() uuid.UUID {
	return uuid.New()
}

// ParseUUID parses a string representation into a UUID.
func ParseUUID(s string) (uuid.UUID, error) {
	return uuid.Parse(s)
}

// ParseUUIDBytes parses a byte slice representation into a UUID.
func ParseUUIDBytes(b []byte) (uuid.UUID, error) {
	return uuid.ParseBytes(b)
}

// ValidateUUID checks if a string is a valid UUID representation.
func ValidateUUID(s string) error {
	return uuid.Validate(s)
}

// FromByteSlice constructs a UUID from a raw 16-byte slice.
func FromByteSlice(b []byte) (uuid.UUID, error) {
	return uuid.FromBytes(b)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/google/uuid@v1.6.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/uuid@v1.6.0"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@11.8.0",
    "runtime": "node@24.13.1"
  }
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"github.com/google/uuid"
)

func main() {
	// 1. uuid.New generates a valid version 4 UUID with RFC4122 variant
	u1 := uuid.New()
	if u1.Version() != 4 {
		fmt.Fprintf(os.Stderr, "expected version 4, got %v\n", u1.Version())
		os.Exit(1)
	}
	if u1.Variant() != uuid.RFC4122 {
		fmt.Fprintf(os.Stderr, "expected RFC4122 variant, got %v\n", u1.Variant())
		os.Exit(1)
	}
	if len(u1.String()) != 36 {
		fmt.Fprintf(os.Stderr, "expected 36 characters string, got %d\n", len(u1.String()))
		os.Exit(1)
	}
	u2 := uuid.New()
	if u1 == u2 {
		fmt.Fprintf(os.Stderr, "expected successive calls to uuid.New to be unique\n")
		os.Exit(1)
	}

	// 2. uuid.Parse and uuid.ParseBytes parse 36-character hyphenated UUID strings and byte slices
	canonicalStr := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
	parsed, err := uuid.Parse(canonicalStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in uuid.Parse: %v\n", err)
		os.Exit(1)
	}
	if parsed.String() != canonicalStr {
		fmt.Fprintf(os.Stderr, "expected parsed string %q, got %q\n", canonicalStr, parsed.String())
		os.Exit(1)
	}
	parsedBytes, err := uuid.ParseBytes([]byte(canonicalStr))
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in uuid.ParseBytes: %v\n", err)
		os.Exit(1)
	}
	if parsedBytes != parsed {
		fmt.Fprintf(os.Stderr, "expected parsedBytes %v, got %v\n", parsed, parsedBytes)
		os.Exit(1)
	}

	// 3. uuid.Parse parses 32-character hexadecimal UUID format and URN formatted strings
	hexStr := "6ba7b8109dad11d180b400c04fd430c8"
	parsedHex, err := uuid.Parse(hexStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error parsing hex string: %v\n", err)
		os.Exit(1)
	}
	if parsedHex != parsed {
		fmt.Fprintf(os.Stderr, "expected hex parse to equal canonical parse, got %v vs %v\n", parsedHex, parsed)
		os.Exit(1)
	}

	urnStr := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
	parsedURN, err := uuid.Parse(urnStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error parsing URN string: %v\n", err)
		os.Exit(1)
	}
	if parsedURN != parsed {
		fmt.Fprintf(os.Stderr, "expected URN parse to equal canonical parse, got %v vs %v\n", parsedURN, parsed)
		os.Exit(1)
	}

	// 4. uuid.Parse and uuid.Validate return errors for malformed or invalid length UUID strings
	invalidStrings := []string{
		"",
		"not-a-valid-uuid",
		"6ba7b810-9dad-11d1-80b4-00c04fd430c",   // 35 chars
		"6ba7b810-9dad-11d1-80b4-00c04fd430c89",  // 37 chars
		"6ba7b810-9dad-11d1-80b4-00c04fd430cg",  // invalid hex 'g'
		"urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c",
	}
	for _, s := range invalidStrings {
		if _, err := uuid.Parse(s); err == nil {
			fmt.Fprintf(os.Stderr, "expected error in uuid.Parse for %q, got nil\n", s)
			os.Exit(1)
		}
		if err := uuid.Validate(s); err == nil {
			fmt.Fprintf(os.Stderr, "expected error in uuid.Validate for %q, got nil\n", s)
			os.Exit(1)
		}
	}
	if err := uuid.Validate(canonicalStr); err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error validating canonical UUID %q: %v\n", canonicalStr, err)
		os.Exit(1)
	}

	// 5. uuid.FromBytes constructs UUID from 16-byte slice and rejects invalid slice lengths
	rawBytes := parsed[:]
	fromBytesUUID, err := uuid.FromBytes(rawBytes)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unexpected error in uuid.FromBytes: %v\n", err)
		os.Exit(1)
	}
	if fromBytesUUID != parsed {
		fmt.Fprintf(os.Stderr, "expected FromBytes UUID %v, got %v\n", parsed, fromBytesUUID)
		os.Exit(1)
	}
	invalidLengths := [][]byte{
		{},
		make([]byte, 15),
		make([]byte, 17),
	}
	for _, b := range invalidLengths {
		if _, err := uuid.FromBytes(b); err == nil {
			fmt.Fprintf(os.Stderr, "expected error in uuid.FromBytes for length %d, got nil\n", len(b))
			os.Exit(1)
		}
	}

	// 6. uuid.Nil represents zero value UUID with Version 0 and Reserved variant
	if uuid.Nil.String() != "00000000-0000-0000-0000-000000000000" {
		fmt.Fprintf(os.Stderr, "unexpected string for Nil UUID: %s\n", uuid.Nil.String())
		os.Exit(1)
	}
	if !bytes.Equal(uuid.Nil[:], make([]byte, 16)) {
		fmt.Fprintf(os.Stderr, "expected all zero bytes for uuid.Nil\n")
		os.Exit(1)
	}
	if uuid.Nil.Version() != 0 {
		fmt.Fprintf(os.Stderr, "expected Nil UUID version 0, got %v\n", uuid.Nil.Version())
		os.Exit(1)
	}
	if uuid.Nil.Variant() != uuid.Reserved {
		fmt.Fprintf(os.Stderr, "expected Nil UUID variant Reserved, got %v\n", uuid.Nil.Variant())
		os.Exit(1)
	}

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

原始种子者

匿名