CodeSampleX

Exemple

github.com/google/uuid v1.6.0: Generate and parse UUIDv4 strings with google/uuid

Échantillon vérifié pour golang github.com/google/uuid v1.6.0: Generate and parse UUIDv4 strings with google/uuid. Le contrat s'est exécuté sur go 1.26 …

sha256:c1f8bf4eb6e96519d90649f09f1f360f42832af56bb8721495b33ea3df94faeb

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
2
Environnement déclaré go 1.26 windows 11 x64 go 1.26 go go 1

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
Generate and parse UUIDv4 strings with google/uuid
Paquets
Symboles
  • NewString
  • Parse
Environnement
go 1.26.5
Créé
2026-08-19T03:47:35Z

Contrat

  1. uuid.NewString generates a non-empty 36-character hyphenated UUID string
  2. uuid.NewString produces distinct unique UUID strings across repeated invocations
  3. uuid.Parse successfully parses standard 36-character UUID strings generated by NewString
  4. uuid.Parse successfully decodes UUID version 4 and RFC4122 variant from NewString output
  5. uuid.Parse parses 32-character hexadecimal UUID strings without hyphens
  6. uuid.Parse parses curly braced UUID strings
  7. uuid.Parse parses URN formatted UUID strings
  8. uuid.Parse returns an error when given invalid or malformed UUID strings
  9. uuid.NewString and uuid.Parse round-trip accurately preserves byte representation

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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: Generate and parse UUIDv4 strings with google/uuid
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/google/uuid@v1.6.0
Demonstrate these symbols/APIs:
  - NewString
  - Parse

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:214a1ec8d69c4cc898588e383e7d0733b1c071477ce34301355261501c5a4e40","contract":["uuid.NewString generates a non-empty 36-character hyphenated UUID string","uuid.NewString produces distinct unique UUID strings across repeated invocations","uuid.Parse successfully parses standard 36-character UUID strings generated by NewString","uuid.Parse successfully decodes UUID version 4 and RFC4122 variant from NewString output","uuid.Parse parses 32-character hexadecimal UUID strings without hyphens","uuid.Parse parses curly braced UUID strings","uuid.Parse parses URN formatted UUID strings","uuid.Parse returns an error when given invalid or malformed UUID strings","uuid.NewString and uuid.Parse round-trip accurately preserves byte representation"],"goal":"Generate and parse UUIDv4 strings with google/uuid","kind":"HOW","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["NewString","Parse"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","ecosystem":"golang","language":"go","os":"windows","osVersionBucket":"11","packageManager":"go","packageManagerVersion":"1.26.5","runtime":"go","runtimeVersion":"1.26.5","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/google/uuid@v1.6.0"],"schemaVersion":1,"symbols":["NewString","Parse"],"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=
main.go
package main

import (
	"fmt"
	"log"

	"github.com/google/uuid"
)

// GenerateUUIDString generates a new random UUID v4 formatted as standard 36-character string.
func GenerateUUIDString() string {
	return uuid.NewString()
}

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

func main() {
	idStr := GenerateUUIDString()
	fmt.Printf("Generated UUID string: %s\n", idStr)

	parsed, err := ParseUUID(idStr)
	if err != nil {
		log.Fatalf("Failed to parse UUID: %v\n", err)
	}
	fmt.Printf("Parsed UUID: %s (Version: %d, Variant: %s)\n", parsed.String(), parsed.Version(), parsed.Variant())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "Generate and parse UUIDv4 strings with google/uuid",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/google/uuid@v1.6.0"
  ],
  "symbols": [
    "NewString",
    "Parse"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"
	"regexp"

	"github.com/google/uuid"
)

func main() {
	// Assertion 1: uuid.NewString generates a non-empty 36-character hyphenated UUID string
	str1 := uuid.NewString()
	if len(str1) != 36 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 36 characters, got %d for %q\n", len(str1), str1)
		os.Exit(1)
	}
	matched, err := regexp.MatchString("^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", str1)
	if err != nil || !matched {
		fmt.Fprintf(os.Stderr, "FAIL: NewString did not match canonical UUIDv4 regex: %q\n", str1)
		os.Exit(1)
	}

	// Assertion 2: uuid.NewString produces distinct unique UUID strings across repeated invocations
	str2 := uuid.NewString()
	if str1 == str2 {
		fmt.Fprintf(os.Stderr, "FAIL: successive NewString calls produced duplicate UUID: %s\n", str1)
		os.Exit(1)
	}

	// Assertion 3: uuid.Parse successfully parses standard 36-character UUID strings generated by NewString
	parsed1, err := uuid.Parse(str1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: uuid.Parse failed on NewString output %q: %v\n", str1, err)
		os.Exit(1)
	}
	if parsed1.String() != str1 {
		fmt.Fprintf(os.Stderr, "FAIL: parsed string mismatch: got %s, want %s\n", parsed1.String(), str1)
		os.Exit(1)
	}

	// Assertion 4: uuid.Parse successfully decodes UUID version 4 and RFC4122 variant from NewString output
	if parsed1.Version() != 4 {
		fmt.Fprintf(os.Stderr, "FAIL: expected UUID version 4, got %d\n", parsed1.Version())
		os.Exit(1)
	}
	if parsed1.Variant() != uuid.RFC4122 {
		fmt.Fprintf(os.Stderr, "FAIL: expected RFC4122 variant, got %v\n", parsed1.Variant())
		os.Exit(1)
	}

	// Assertion 5: uuid.Parse parses 32-character hexadecimal UUID strings without hyphens
	hexStr := "6ba7b8109dad11d180b400c04fd430c8"
	parsedHex, err := uuid.Parse(hexStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: uuid.Parse failed on hex string %q: %v\n", hexStr, err)
		os.Exit(1)
	}
	if parsedHex.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
		fmt.Fprintf(os.Stderr, "FAIL: hex parsed UUID string mismatch: got %s\n", parsedHex.String())
		os.Exit(1)
	}

	// Assertion 6: uuid.Parse parses curly braced UUID strings
	bracedStr := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
	parsedBraced, err := uuid.Parse(bracedStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: uuid.Parse failed on braced string %q: %v\n", bracedStr, err)
		os.Exit(1)
	}
	if parsedBraced != parsedHex {
		fmt.Fprintf(os.Stderr, "FAIL: braced parsed UUID mismatch\n")
		os.Exit(1)
	}

	// Assertion 7: uuid.Parse parses URN formatted UUID strings
	urnStr := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
	parsedURN, err := uuid.Parse(urnStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: uuid.Parse failed on URN string %q: %v\n", urnStr, err)
		os.Exit(1)
	}
	if parsedURN != parsedHex {
		fmt.Fprintf(os.Stderr, "FAIL: URN parsed UUID mismatch\n")
		os.Exit(1)
	}

	// Assertion 8: uuid.Parse returns an error when given invalid or malformed UUID strings
	invalidSamples := []string{
		"",
		"invalid-uuid-string",
		"6ba7b810-9dad-11d1-80b4-00c04fd430c",     // 35 chars
		"6ba7b810-9dad-11d1-80b4-00c04fd430c89",    // 37 chars
		"6ba7b810-9dad-11d1-80b4-00c04fd430cg",    // invalid hex 'g'
		"{6ba7b810-9dad-11d1-80b4-00c04fd430c8",    // unmatched brace
	}
	for _, inv := range invalidSamples {
		if _, err := uuid.Parse(inv); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error for invalid UUID string %q, got nil\n", inv)
			os.Exit(1)
		}
	}

	// Assertion 9: uuid.NewString and uuid.Parse round-trip accurately preserves byte representation
	origStr := uuid.NewString()
	parsedRound, err := uuid.Parse(origStr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: round-trip parse error: %v\n", err)
		os.Exit(1)
	}
	if parsedRound.String() != origStr {
		fmt.Fprintf(os.Stderr, "FAIL: round-trip string mismatch: got %s, want %s\n", parsedRound.String(), origStr)
		os.Exit(1)
	}
	var rawBytes [16]byte
	copy(rawBytes[:], parsedRound[:])
	if !bytes.Equal(parsedRound[:], rawBytes[:]) {
		fmt.Fprintf(os.Stderr, "FAIL: round-trip byte mismatch\n")
		os.Exit(1)
	}

	fmt.Println("PASS: all contract assertions passed for NewString and Parse")
}

Seeder d'origine

anonyme