CodeSampleX

示例

golang.org/x/text v0.34.0: collate.New, currency.ParseISO, message.NewPrinter

已验证示例 — golang golang.org/x/text v0.34.0: collate.New, currency.ParseISO, message.NewPrinter. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过.

sha256:c71e27206929b43aca63aa14c6c8150c8ae866b9022aff01c1822eae6366e362

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify pkg:golang/golang.org/x/text@v0.34.0
包
符号
  • collate.New
  • currency.ParseISO
  • message.NewPrinter
  • precis.UsernameCaseMapped
  • search.New
创建时间
2026-08-24T20:11:33Z

契约

  1. collate.New creates locale-sensitive collator comparing strings according to language collation rules
  2. search.New and Matcher.CompileString perform locale-aware and case-insensitive substring searching
  3. message.NewPrinter formats localized messages and numbers with language catalogs
  4. currency.ParseISO resolves ISO-4217 currency units and creates currency amounts
  5. precis.UsernameCaseMapped enforces username profile canonicalization and case-insensitive comparison

文件

  • 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/golang.org/x/text@v0.34.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/text@v0.34.0
Demonstrate these symbols/APIs:
  - collate.New
  - currency.ParseISO
  - message.NewPrinter
  - precis.UsernameCaseMapped
  - search.New
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:109e83e4b78be6c94ec199f8e6a5e551f56038379eb8ac24c5fe2b4f89b7bce5","contract":["collate.New creates locale-sensitive collator comparing strings according to language collation rules","search.New and Matcher.CompileString perform locale-aware and case-insensitive substring searching","message.NewPrinter formats localized messages and numbers with language catalogs","currency.ParseISO resolves ISO-4217 currency units and creates currency amounts","precis.UsernameCaseMapped enforces username profile canonicalization and case-insensitive comparison"],"goal":"verify pkg:golang/golang.org/x/text@v0.34.0","kind":"HOW","packages":["pkg:golang/golang.org/x/text@v0.34.0"],"schemaVersion":1,"symbols":["collate.New","currency.ParseISO","message.NewPrinter","precis.UsernameCaseMapped","search.New"]},"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/golang.org/x/text@v0.34.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/text@v0.34.0","symbols":["collate.New","currency.ParseISO","message.NewPrinter","precis.UsernameCaseMapped","search.New"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.24.0

require golang.org/x/text v0.34.0
go.sum
golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk=
golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA=
main.go
package main

import (
	"fmt"
	"os"

	"golang.org/x/text/collate"
	"golang.org/x/text/currency"
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/search"
	"golang.org/x/text/secure/precis"
)

// CompareLocalized compares two strings using locale-specific collation rules.
func CompareLocalized(tag language.Tag, a, b string) int {
	c := collate.New(tag)
	return c.CompareString(a, b)
}

// FindSubstring searches for pattern in text using locale-sensitive matcher.
func FindSubstring(tag language.Tag, patternStr, text string) (int, int) {
	m := search.New(tag, search.IgnoreCase)
	pattern := m.CompileString(patternStr)
	return pattern.IndexString(text)
}

// FormatLocalizedMessage formats a greeting string with locale-specific number formatting.
func FormatLocalizedMessage(tag language.Tag, name string, amount float64) string {
	p := message.NewPrinter(tag)
	return p.Sprintf("Hello %s, balance: %.2f", name, amount)
}

// ParseCurrency parses an ISO-4217 code and returns its standard unit representation.
func ParseCurrency(iso string) (currency.Unit, error) {
	return currency.ParseISO(iso)
}

// NormalizeUser applies PRECIS username profile normalization.
func NormalizeUser(raw string) (string, error) {
	return precis.UsernameCaseMapped.String(raw)
}

func main() {
	// Setup localized greeting message catalog
	_ = message.SetString(language.German, "Hello %s, balance: %.2f", "Hallo %s, Kontostand: %.2f")

	// 1. Collation
	cmp := CompareLocalized(language.Swedish, "z", "ö")
	fmt.Printf("Swedish collation comparison ('z' vs 'ö'): %d\n", cmp)

	// 2. Search
	start, end := FindSubstring(language.English, "world", "Hello World!")
	fmt.Printf("Search match indices: %d..%d\n", start, end)

	// 3. Message formatting
	msgDE := FormatLocalizedMessage(language.German, "Alex", 1234.56)
	fmt.Printf("German message: %s\n", msgDE)

	// 4. Currency
	unit, err := ParseCurrency("USD")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Currency error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Parsed currency: %s\n", unit)

	// 5. PRECIS username normalization
	norm, err := NormalizeUser("Admin_User")
	if err != nil {
		fmt.Fprintf(os.Stderr, "PRECIS error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Normalized username: %s\n", norm)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/text@v0.34.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/text@v0.34.0"
  ],
  "symbols": [
    "collate.New",
    "currency.ParseISO",
    "message.NewPrinter",
    "precis.UsernameCaseMapped",
    "search.New"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"

	"golang.org/x/text/collate"
	"golang.org/x/text/currency"
	"golang.org/x/text/language"
	"golang.org/x/text/message"
	"golang.org/x/text/search"
	"golang.org/x/text/secure/precis"
)

func main() {
	// Assertion 1: collate.New creates locale-sensitive collator comparing strings according to language collation rules
	cSwedish := collate.New(language.Swedish)
	cGerman := collate.New(language.German)
	if cSwedish.CompareString("z", "ö") >= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: Swedish collation failed: 'z' should sort before 'ö'\n")
		os.Exit(1)
	}
	if cGerman.CompareString("ö", "z") >= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: German collation failed: 'ö' should sort before 'z'\n")
		os.Exit(1)
	}
	var buf collate.Buffer
	key := cGerman.KeyFromString(&buf, "sample")
	if len(key) == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: collate.KeyFromString produced empty sort key\n")
		os.Exit(1)
	}

	// Assertion 2: search.New and Matcher.CompileString perform locale-aware and case-insensitive substring searching
	matcher := search.New(language.English, search.IgnoreCase)
	pattern := matcher.CompileString("world")
	start, end := pattern.IndexString("Hello World!")
	if start != 6 || end != 11 {
		fmt.Fprintf(os.Stderr, "FAIL: search.IndexString mismatch: expected (6, 11), got (%d, %d)\n", start, end)
		os.Exit(1)
	}
	if notFoundStart, _ := pattern.IndexString("Hello Universe!"); notFoundStart != -1 {
		fmt.Fprintf(os.Stderr, "FAIL: search.IndexString should return -1 for unmatched text\n")
		os.Exit(1)
	}

	// Assertion 3: message.NewPrinter formats localized messages and numbers with language catalogs
	err := message.SetString(language.German, "Hello %s", "Hallo %s")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: message.SetString error: %v\n", err)
		os.Exit(1)
	}
	pDE := message.NewPrinter(language.German)
	greeting := pDE.Sprintf("Hello %s", "Welt")
	if greeting != "Hallo Welt" {
		fmt.Fprintf(os.Stderr, "FAIL: message.Sprintf greeting mismatch: got %q\n", greeting)
		os.Exit(1)
	}
	numDE := pDE.Sprintf("%.2f", 1234.56)
	if numDE != "1.234,56" {
		fmt.Fprintf(os.Stderr, "FAIL: German number formatting mismatch: got %q, expected '1.234,56'\n", numDE)
		os.Exit(1)
	}
	pUS := message.NewPrinter(language.English)
	numUS := pUS.Sprintf("%.2f", 1234.56)
	if numUS != "1,234.56" {
		fmt.Fprintf(os.Stderr, "FAIL: US number formatting mismatch: got %q, expected '1,234.56'\n", numUS)
		os.Exit(1)
	}

	// Assertion 4: currency.ParseISO resolves ISO-4217 currency units and creates currency amounts
	usd, err := currency.ParseISO("USD")
	if err != nil || usd.String() != "USD" {
		fmt.Fprintf(os.Stderr, "FAIL: currency.ParseISO('USD') failed: %v\n", err)
		os.Exit(1)
	}
	eur, err := currency.ParseISO("EUR")
	if err != nil || eur.String() != "EUR" {
		fmt.Fprintf(os.Stderr, "FAIL: currency.ParseISO('EUR') failed: %v\n", err)
		os.Exit(1)
	}
	amtUSD := usd.Amount(150.75)
	if amtUSD.Currency() != usd {
		fmt.Fprintf(os.Stderr, "FAIL: Amount.Currency mismatch for USD\n")
		os.Exit(1)
	}

	// Assertion 5: precis.UsernameCaseMapped enforces username profile canonicalization and case-insensitive comparison
	profile := precis.UsernameCaseMapped
	normUser, err := profile.String("Admin_User")
	if err != nil || normUser != "admin_user" {
		fmt.Fprintf(os.Stderr, "FAIL: precis username normalization mismatch: %v, got %q\n", err, normUser)
		os.Exit(1)
	}
	if !profile.Compare("Admin_User", "admin_user") {
		fmt.Fprintf(os.Stderr, "FAIL: precis profile.Compare failed for case-equivalent usernames\n")
		os.Exit(1)
	}

	fmt.Println("PASS: pkg:golang/golang.org/x/text@v0.34.0 contract verified")
}

原始种子者

匿名