Beispiel
github.com/spf13/cast v1.10.0: ToInt
Verifiziertes Beispiel für golang github.com/spf13/cast v1.10.0: ToInt. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker und bestand.
sha256:ae4c33c6f55347312c95d3a6f33bb549662141519ca4ed918e6dcb265fc7c8cb
Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen.
Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen.
MIT-0
Ausführungsbelege
Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.
- Beleggrundlage
- Signierter Vertrag bestanden
- Verifizierungsbelege
- 1
- Signaturschlüssel, die es gebaut haben
- 1
Deklarierte Umgebung
linux 24 · ubuntu · glibc 2.39 x64 go
Umgebungen der Verifizierungsläufe
| Umgebung | Contract | Stufen | Lauf |
|---|---|---|---|
| go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a… |
2026-09-06 |
Fall
HOW- Ziel
- verify pkg:golang/github.com/spf13/cast@v1.10.0
- Symbole
-
- github.com/spf13/cast.ToInt
- Erstellt
- 2026-09-06T16:26:49Z
Contract
- cast.ToInt converts integer, unsigned integer, and floating-point numeric values to int
- cast.ToInt parses decimal and signed numeric strings into int
- cast.ToInt converts boolean values to 1 for true and 0 for false
- cast.ToInt returns 0 for nil values
- cast.ToInt unwraps pointer values before converting their underlying values to int
- cast.ToInt returns 0 and cast.ToIntE returns an error for invalid numeric strings and unsupported types
- ToInt converts the given value to an int using cast.ToInt
Dateien
- PROMPT.md
- cast_int.go
- contract_test.go
- csx.json
- go.mod
- go.sum
- spec.json
Quelltext
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/spf13/cast@v1.10.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/github.com/spf13/cast@v1.10.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.
package sample
import (
"github.com/spf13/cast"
)
// ToInt converts an arbitrary value into an int using cast.ToInt.
// It returns 0 if conversion cannot be performed.
func ToInt(v any) int {
return cast.ToInt(v)
}
// ToIntE converts an arbitrary value into an int using cast.ToIntE,
// returning an error if conversion cannot be performed.
func ToIntE(v any) (int, error) {
return cast.ToIntE(v)
}
package sample
import (
"testing"
"github.com/spf13/cast"
)
func TestToInt_NumericValues(t *testing.T) {
if got := cast.ToInt(42); got != 42 {
t.Fatalf("cast.ToInt(42) = %d; want 42", got)
}
if got := cast.ToInt(int8(-8)); got != -8 {
t.Fatalf("cast.ToInt(int8(-8)) = %d; want -8", got)
}
if got := cast.ToInt(int16(-16)); got != -16 {
t.Fatalf("cast.ToInt(int16(-16)) = %d; want -16", got)
}
if got := cast.ToInt(int32(-32)); got != -32 {
t.Fatalf("cast.ToInt(int32(-32)) = %d; want -32", got)
}
if got := cast.ToInt(int64(-64)); got != -64 {
t.Fatalf("cast.ToInt(int64(-64)) = %d; want -64", got)
}
if got := cast.ToInt(uint(100)); got != 100 {
t.Fatalf("cast.ToInt(uint(100)) = %d; want 100", got)
}
if got := cast.ToInt(uint8(200)); got != 200 {
t.Fatalf("cast.ToInt(uint8(200)) = %d; want 200", got)
}
if got := cast.ToInt(uint16(300)); got != 300 {
t.Fatalf("cast.ToInt(uint16(300)) = %d; want 300", got)
}
if got := cast.ToInt(uint32(400)); got != 400 {
t.Fatalf("cast.ToInt(uint32(400)) = %d; want 400", got)
}
if got := cast.ToInt(uint64(500)); got != 500 {
t.Fatalf("cast.ToInt(uint64(500)) = %d; want 500", got)
}
if got := cast.ToInt(3.14); got != 3 {
t.Fatalf("cast.ToInt(3.14) = %d; want 3", got)
}
if got := cast.ToInt(float32(9.99)); got != 9 {
t.Fatalf("cast.ToInt(float32(9.99)) = %d; want 9", got)
}
}
func TestToInt_Strings(t *testing.T) {
if got := cast.ToInt("12345"); got != 12345 {
t.Fatalf("cast.ToInt(\"12345\") = %d; want 12345", got)
}
if got := cast.ToInt("-6789"); got != -6789 {
t.Fatalf("cast.ToInt(\"-6789\") = %d; want -6789", got)
}
if got := cast.ToInt("0"); got != 0 {
t.Fatalf("cast.ToInt(\"0\") = %d; want 0", got)
}
}
func TestToInt_Booleans(t *testing.T) {
if got := cast.ToInt(true); got != 1 {
t.Fatalf("cast.ToInt(true) = %d; want 1", got)
}
if got := cast.ToInt(false); got != 0 {
t.Fatalf("cast.ToInt(false) = %d; want 0", got)
}
}
func TestToInt_Nil(t *testing.T) {
if got := cast.ToInt(nil); got != 0 {
t.Fatalf("cast.ToInt(nil) = %d; want 0", got)
}
val, err := cast.ToIntE(nil)
if err != nil || val != 0 {
t.Fatalf("cast.ToIntE(nil) = (%d, %v); want (0, nil)", val, err)
}
}
func TestToInt_Pointers(t *testing.T) {
n := 777
if got := cast.ToInt(&n); got != 777 {
t.Fatalf("cast.ToInt(&n) = %d; want 777", got)
}
s := "888"
if got := cast.ToInt(&s); got != 888 {
t.Fatalf("cast.ToInt(&s) = %d; want 888", got)
}
b := true
if got := cast.ToInt(&b); got != 1 {
t.Fatalf("cast.ToInt(&b) = %d; want 1", got)
}
}
func TestToInt_InvalidAndUnsupported(t *testing.T) {
invalidCases := []any{
"not_a_number",
[]int{1, 2, 3},
map[string]int{"a": 1},
struct{}{},
}
for _, c := range invalidCases {
if got := cast.ToInt(c); got != 0 {
t.Fatalf("cast.ToInt(%v) = %d; want 0", c, got)
}
if _, err := cast.ToIntE(c); err == nil {
t.Fatalf("cast.ToIntE(%v) expected error, got nil", c)
}
}
}
func TestToInt_Helper(t *testing.T) {
cases := []struct {
input any
expected int
}{
{"42", 42},
{-100, -100},
{true, 1},
{false, 0},
{nil, 0},
{3.14, 3},
}
for _, c := range cases {
if got := ToInt(c.input); got != c.expected {
t.Fatalf("ToInt(%v) = %d; want %d", c.input, got, c.expected)
}
val, err := ToIntE(c.input)
if err != nil || val != c.expected {
t.Fatalf("ToIntE(%v) = (%d, %v); want (%d, nil)", c.input, val, err, c.expected)
}
}
}
{"case":{"caseId":"case:sha256:7cbcd21a788a6ae87d25cd8c458838d2411d5b7c6d15c143ccb787567d9f3cd7","contract":["cast.ToInt converts integer, unsigned integer, and floating-point numeric values to int","cast.ToInt parses decimal and signed numeric strings into int","cast.ToInt converts boolean values to 1 for true and 0 for false","cast.ToInt returns 0 for nil values","cast.ToInt unwraps pointer values before converting their underlying values to int","cast.ToInt returns 0 and cast.ToIntE returns an error for invalid numeric strings and unsupported types","ToInt converts the given value to an int using cast.ToInt"],"goal":"verify pkg:golang/github.com/spf13/cast@v1.10.0","kind":"HOW","packages":["pkg:golang/github.com/spf13/cast@v1.10.0"],"schemaVersion":1,"symbols":["github.com/spf13/cast.ToInt"]},"contractCommand":["go","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/spf13/cast@v1.10.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/spf13/cast@v1.10.0","symbols":["github.com/spf13/cast.ToInt"],"verifierAdapter":"golang@1"}
module example.com/sample
go 1.26.6
require github.com/spf13/cast v1.10.0
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
{
"schemaVersion": 1,
"goal": "verify pkg:golang/github.com/spf13/cast@v1.10.0",
"kind": "HOW",
"packages": [
"pkg:golang/github.com/spf13/cast@v1.10.0"
]
}
Ursprungs-Seeder
anonym