CodeSampleX

샘플

golang.org/x/text v0.11.0: collate string comparison, message localized formatting, and currency parsing with golang.org/x/text

검증된 샘플 — golang golang.org/x/text v0.11.0: collate string comparison, message localized formatting, and currency parsing with golang.org/x/text. go 1.26 …

sha256:183d631c768060468ca46e0e4a49eba6886cdc8dc2e13adcc4449ee1a92da671

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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-27

케이스

HOW
목표
collate string comparison, message localized formatting, and currency parsing with golang.org/x/text
패키지
심벌
  • golang.org/x/text/collate.Collator.CompareString
  • golang.org/x/text/collate.New
  • golang.org/x/text/currency.FromTag
  • golang.org/x/text/currency.ParseISO
  • golang.org/x/text/message.NewPrinter
생성일
2026-08-27T12:18:46Z

컨트랙트

  1. collate.New and Collator.CompareString perform language-sensitive and case-insensitive string collation
  2. collate.New respects locale-specific alphabet ordering differences across languages
  3. currency.ParseISO parses standard ISO-4217 three-letter currency codes into Unit values
  4. currency.FromTag resolves regional default currency units from BCP 47 language tags
  5. message.NewPrinter formats localized integers and decimals with locale-specific separators

파일

  • 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: collate string comparison, message localized formatting, and currency parsing with golang.org/x/text
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/text@v0.11.0
Demonstrate these symbols/APIs:
  - golang.org/x/text/collate.Collator.CompareString
  - golang.org/x/text/collate.New
  - golang.org/x/text/currency.FromTag
  - golang.org/x/text/currency.ParseISO
  - golang.org/x/text/message.NewPrinter

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:2f97eea63b6ebab68319e85a15a4b0f4d0ab65fa0af27a91c10257b25950de46","contract":["collate.New and Collator.CompareString perform language-sensitive and case-insensitive string collation","collate.New respects locale-specific alphabet ordering differences across languages","currency.ParseISO parses standard ISO-4217 three-letter currency codes into Unit values","currency.FromTag resolves regional default currency units from BCP 47 language tags","message.NewPrinter formats localized integers and decimals with locale-specific separators"],"goal":"collate string comparison, message localized formatting, and currency parsing with golang.org/x/text","kind":"HOW","packages":["pkg:golang/golang.org/x/text@v0.11.0"],"schemaVersion":1,"symbols":["golang.org/x/text/collate.Collator.CompareString","golang.org/x/text/collate.New","golang.org/x/text/currency.FromTag","golang.org/x/text/currency.ParseISO","golang.org/x/text/message.NewPrinter"]},"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.11.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/text@v0.11.0","symbols":["golang.org/x/text/collate.Collator.CompareString","golang.org/x/text/collate.New","golang.org/x/text/currency.FromTag","golang.org/x/text/currency.ParseISO","golang.org/x/text/message.NewPrinter"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/text v0.11.0
go.sum
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
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"
)

// CompareStringsCollate compares two strings using a language collator with optional settings.
func CompareStringsCollate(tag language.Tag, a, b string, ignoreCase bool) int {
	var c *collate.Collator
	if ignoreCase {
		c = collate.New(tag, collate.IgnoreCase)
	} else {
		c = collate.New(tag)
	}
	return c.CompareString(a, b)
}

// ParseCurrencyCode parses an ISO 4217 currency code.
func ParseCurrencyCode(code string) (currency.Unit, error) {
	return currency.ParseISO(code)
}

// ResolveRegionalCurrency determines the most likely currency for a given BCP 47 language tag.
func ResolveRegionalCurrency(tag language.Tag) (currency.Unit, language.Confidence) {
	return currency.FromTag(tag)
}

// FormatLocalizedNumber formats integer and float values according to language locale conventions.
func FormatLocalizedNumber(tag language.Tag, intVal int64, floatVal float64) (string, string) {
	p := message.NewPrinter(tag)
	return p.Sprintf("%d", intVal), p.Sprintf("%.2f", floatVal)
}

func main() {
	// 1. Collation
	cmpEn := CompareStringsCollate(language.English, "apple", "APPLE", true)
	fmt.Printf("English case-insensitive collation 'apple' vs 'APPLE': %d\n", cmpEn)

	cmpDe := CompareStringsCollate(language.German, "äpfel", "zitrone", false)
	fmt.Printf("German collation 'äpfel' vs 'zitrone': %d\n", cmpDe)

	// 2. Currency ISO parsing
	eur, err := ParseCurrencyCode("EUR")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Currency ISO parse error: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Parsed ISO code 'EUR': %v\n", eur)

	// 3. Currency from Tag
	usCurrency, conf := ResolveRegionalCurrency(language.AmericanEnglish)
	fmt.Printf("US tag currency: %v (confidence: %v)\n", usCurrency, conf)

	// 4. Localized message / number formatting
	intEn, floatEn := FormatLocalizedNumber(language.English, 1234567, 1234.56)
	fmt.Printf("English formatted int: %s, float: %s\n", intEn, floatEn)

	intDe, floatDe := FormatLocalizedNumber(language.German, 1234567, 1234.56)
	fmt.Printf("German formatted int: %s, float: %s\n", intDe, floatDe)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "collate string comparison, message localized formatting, and currency parsing with golang.org/x/text",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/text@v0.11.0"
  ],
  "symbols": [
    "golang.org/x/text/collate.Collator.CompareString",
    "golang.org/x/text/collate.New",
    "golang.org/x/text/currency.FromTag",
    "golang.org/x/text/currency.ParseISO",
    "golang.org/x/text/message.NewPrinter"
  ]
}
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"
)

func main() {
	// Assertion 1: collate.New and Collator.CompareString perform language-sensitive and case-insensitive string collation
	cCaseInsensitive := collate.New(language.English, collate.IgnoreCase)
	if res := cCaseInsensitive.CompareString("apple", "APPLE"); res != 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected CompareString('apple', 'APPLE') with IgnoreCase == 0, got %d\n", res)
		os.Exit(1)
	}
	if res := cCaseInsensitive.CompareString("apple", "banana"); res >= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 'apple' < 'banana', got %d\n", res)
		os.Exit(1)
	}
	if res := cCaseInsensitive.CompareString("banana", "apple"); res <= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 'banana' > 'apple', got %d\n", res)
		os.Exit(1)
	}

	// Assertion 2: collate.New respects locale-specific alphabet ordering differences across languages
	cGerman := collate.New(language.German)
	cSwedish := collate.New(language.Swedish)
	if res := cGerman.CompareString("äpfel", "zitrone"); res >= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected German 'äpfel' < 'zitrone', got %d\n", res)
		os.Exit(1)
	}
	if res := cSwedish.CompareString("äpple", "zebra"); res <= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: expected Swedish 'äpple' > 'zebra', got %d\n", res)
		os.Exit(1)
	}

	// Assertion 3: currency.ParseISO parses standard ISO-4217 three-letter currency codes into Unit values
	eur, err := currency.ParseISO("EUR")
	if err != nil || eur != currency.EUR {
		fmt.Fprintf(os.Stderr, "FAIL: currency.ParseISO('EUR') = %v, err=%v\n", eur, err)
		os.Exit(1)
	}
	usd, err := currency.ParseISO("USD")
	if err != nil || usd != currency.USD {
		fmt.Fprintf(os.Stderr, "FAIL: currency.ParseISO('USD') = %v, err=%v\n", usd, err)
		os.Exit(1)
	}
	if _, err := currency.ParseISO("INVALID"); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error for invalid ISO currency code\n")
		os.Exit(1)
	}

	// Assertion 4: currency.FromTag resolves regional default currency units from BCP 47 language tags
	curUS, confUS := currency.FromTag(language.AmericanEnglish)
	if curUS != currency.USD || confUS != language.Exact {
		fmt.Fprintf(os.Stderr, "FAIL: currency.FromTag(AmericanEnglish) = %v, conf=%v, expected USD Exact\n", curUS, confUS)
		os.Exit(1)
	}
	curJP, confJP := currency.FromTag(language.Make("ja-JP"))
	if curJP != currency.JPY || confJP != language.Exact {
		fmt.Fprintf(os.Stderr, "FAIL: currency.FromTag(ja-JP) = %v, conf=%v, expected JPY Exact\n", curJP, confJP)
		os.Exit(1)
	}
	curUK, confUK := currency.FromTag(language.BritishEnglish)
	if curUK != currency.GBP || confUK != language.Exact {
		fmt.Fprintf(os.Stderr, "FAIL: currency.FromTag(BritishEnglish) = %v, conf=%v, expected GBP Exact\n", curUK, confUK)
		os.Exit(1)
	}

	// Assertion 5: message.NewPrinter formats localized integers and decimals with locale-specific separators
	pEn := message.NewPrinter(language.English)
	if s := pEn.Sprintf("%d", 1234567); s != "1,234,567" {
		fmt.Fprintf(os.Stderr, "FAIL: English int formatting mismatch: got %q, expected '1,234,567'\n", s)
		os.Exit(1)
	}
	if s := pEn.Sprintf("%.2f", 1234.56); s != "1,234.56" {
		fmt.Fprintf(os.Stderr, "FAIL: English float formatting mismatch: got %q, expected '1,234.56'\n", s)
		os.Exit(1)
	}

	pDe := message.NewPrinter(language.German)
	if s := pDe.Sprintf("%d", 1234567); s != "1.234.567" {
		fmt.Fprintf(os.Stderr, "FAIL: German int formatting mismatch: got %q, expected '1.234.567'\n", s)
		os.Exit(1)
	}
	if s := pDe.Sprintf("%.2f", 1234.56); s != "1.234,56" {
		fmt.Fprintf(os.Stderr, "FAIL: German float formatting mismatch: got %q, expected '1.234,56'\n", s)
		os.Exit(1)
	}

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

오리진 시더

익명