CodeSampleX

示例

github.com/shopspring/decimal v1.2.0

已验证示例 — golang github.com/shopspring/decimal v1.2.0. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: decimal.NewFromString parses exact decimal…

sha256:98b0dda6842a7f6abb3056cfe3d42d04422934afcd892938cededc2a6b0c52d1

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 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-28

案例

HOW
目标
verify pkg:golang/github.com/shopspring/decimal@v1.2.0
包
创建时间
2026-08-28T09:15:43Z

契约

  1. decimal.NewFromString parses exact decimal representations without binary floating-point inaccuracy
  2. decimal.Decimal arithmetic operations Add, Sub, Mul, and DivRound perform exact arithmetic
  3. decimal.Decimal rounding methods Round and RoundBank round to specified precision using standard and banker's rounding
  4. decimal.Decimal comparison methods Equal, GreaterThan, and LessThan compare numeric values independently of scale representation
  5. decimal.Decimal MarshalJSON and UnmarshalJSON serialize and deserialize decimal values as valid JSON numbers

文件

  • 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/github.com/shopspring/decimal@v1.2.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/shopspring/decimal@v1.2.0

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:3b3b5693b834c41119956024afd1b504ffaa00b081d1f3ddbb383ccbbd2aac3b","contract":["decimal.NewFromString parses exact decimal representations without binary floating-point inaccuracy","decimal.Decimal arithmetic operations Add, Sub, Mul, and DivRound perform exact arithmetic","decimal.Decimal rounding methods Round and RoundBank round to specified precision using standard and banker's rounding","decimal.Decimal comparison methods Equal, GreaterThan, and LessThan compare numeric values independently of scale representation","decimal.Decimal MarshalJSON and UnmarshalJSON serialize and deserialize decimal values as valid JSON numbers"],"goal":"verify pkg:golang/github.com/shopspring/decimal@v1.2.0","kind":"HOW","packages":["pkg:golang/github.com/shopspring/decimal@v1.2.0"],"schemaVersion":1},"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/github.com/shopspring/decimal@v1.2.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/shopspring/decimal@v1.2.0","verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/shopspring/decimal v1.2.0
go.sum
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
main.go
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/shopspring/decimal"
)

// FinancialCalculator demonstrates precision arithmetic and rounding with decimal.Decimal.
type FinancialCalculator struct {
	baseAmount decimal.Decimal
}

// NewFinancialCalculator creates a calculator with a base decimal amount.
func NewFinancialCalculator(amountStr string) (FinancialCalculator, error) {
	d, err := decimal.NewFromString(amountStr)
	if err != nil {
		return FinancialCalculator{}, err
	}
	return FinancialCalculator{baseAmount: d}, nil
}

// AddTax calculates the total after adding a percentage tax.
func (fc FinancialCalculator) AddTax(rateStr string) (decimal.Decimal, error) {
	rate, err := decimal.NewFromString(rateStr)
	if err != nil {
		return decimal.Zero, err
	}
	tax := fc.baseAmount.Mul(rate)
	return fc.baseAmount.Add(tax), nil
}

// SplitEvenly divides the base amount into n parts rounded to 2 decimal places.
func (fc FinancialCalculator) SplitEvenly(parts int64) decimal.Decimal {
	divisor := decimal.NewFromInt(parts)
	return fc.baseAmount.DivRound(divisor, 2)
}

func main() {
	calc, err := NewFinancialCalculator("100.50")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to initialize calculator: %v\n", err)
		os.Exit(1)
	}

	totalWithTax, err := calc.AddTax("0.0825")
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to calculate tax: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("Total with tax: %s\n", totalWithTax.StringFixed(2))

	share := calc.SplitEvenly(3)
	fmt.Printf("Split into 3: %s\n", share.StringFixed(2))

	data, err := json.Marshal(map[string]decimal.Decimal{"total": totalWithTax})
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to marshal json: %v\n", err)
		os.Exit(1)
	}
	fmt.Printf("JSON: %s\n", string(data))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/shopspring/decimal@v1.2.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/shopspring/decimal@v1.2.0"
  ]
}
test/contract.go
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/shopspring/decimal"
)

func main() {
	// Assertion 1: decimal.NewFromString parses exact decimal representations without binary floating-point inaccuracy
	d1, err := decimal.NewFromString("0.1")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: decimal.NewFromString failed: %v\n", err)
		os.Exit(1)
	}
	d2, err := decimal.NewFromString("0.2")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: decimal.NewFromString failed: %v\n", err)
		os.Exit(1)
	}
	sum := d1.Add(d2)
	expectedSum, _ := decimal.NewFromString("0.3")
	if !sum.Equal(expectedSum) {
		fmt.Fprintf(os.Stderr, "FAIL: 0.1 + 0.2 != 0.3, got: %s\n", sum.String())
		os.Exit(1)
	}

	// Assertion 2: decimal.Decimal arithmetic operations Add, Sub, Mul, and DivRound perform exact arithmetic
	price, _ := decimal.NewFromString("19.99")
	quantity, _ := decimal.NewFromString("3")
	subtotal := price.Mul(quantity)
	discount, _ := decimal.NewFromString("5.00")
	total := subtotal.Sub(discount)
	if total.String() != "54.97" {
		fmt.Fprintf(os.Stderr, "FAIL: arithmetic mismatch: got %s, expected 54.97\n", total.String())
		os.Exit(1)
	}
	split := total.DivRound(decimal.NewFromInt(4), 2)
	if split.StringFixed(2) != "13.74" {
		fmt.Fprintf(os.Stderr, "FAIL: DivRound mismatch: got %s, expected 13.74\n", split.StringFixed(2))
		os.Exit(1)
	}

	// Assertion 3: decimal.Decimal rounding methods Round and RoundBank round to specified precision using standard and banker's rounding
	valHalfUp, _ := decimal.NewFromString("2.345")
	if rounded := valHalfUp.Round(2).String(); rounded != "2.35" {
		fmt.Fprintf(os.Stderr, "FAIL: Round mismatch: got %s, expected 2.35\n", rounded)
		os.Exit(1)
	}
	valBank1, _ := decimal.NewFromString("2.345")
	if roundBank := valBank1.RoundBank(2).String(); roundBank != "2.34" {
		fmt.Fprintf(os.Stderr, "FAIL: RoundBank(2.345) mismatch: got %s, expected 2.34\n", roundBank)
		os.Exit(1)
	}
	valBank2, _ := decimal.NewFromString("2.355")
	if roundBank := valBank2.RoundBank(2).String(); roundBank != "2.36" {
		fmt.Fprintf(os.Stderr, "FAIL: RoundBank(2.355) mismatch: got %s, expected 2.36\n", roundBank)
		os.Exit(1)
	}

	// Assertion 4: decimal.Decimal comparison methods Equal, GreaterThan, and LessThan compare numeric values independently of scale representation
	a, _ := decimal.NewFromString("100.500")
	b, _ := decimal.NewFromString("100.5")
	c, _ := decimal.NewFromString("100.51")
	if !a.Equal(b) {
		fmt.Fprintf(os.Stderr, "FAIL: Equal failed for trailing zeros: %s vs %s\n", a.String(), b.String())
		os.Exit(1)
	}
	if !c.GreaterThan(a) || !a.LessThan(c) {
		fmt.Fprintf(os.Stderr, "FAIL: comparison failed: a=%s, c=%s\n", a.String(), c.String())
		os.Exit(1)
	}

	// Assertion 5: decimal.Decimal MarshalJSON and UnmarshalJSON serialize and deserialize decimal values as valid JSON numbers
	type Item struct {
		Price decimal.Decimal `json:"price"`
	}
	item := Item{Price: price}
	jsonBytes, err := json.Marshal(item)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: MarshalJSON failed: %v\n", err)
		os.Exit(1)
	}
	var unmarshaled Item
	if err := json.Unmarshal(jsonBytes, &unmarshaled); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalJSON failed: %v\n", err)
		os.Exit(1)
	}
	if !unmarshaled.Price.Equal(price) {
		fmt.Fprintf(os.Stderr, "FAIL: UnmarshalJSON value mismatch: got %s, expected %s\n", unmarshaled.Price.String(), price.String())
		os.Exit(1)
	}

	fmt.Println("PASS: pkg:golang/github.com/shopspring/decimal@v1.2.0 contract verified")
}

原始种子者

匿名