示例
github.com/go-logfmt/logfmt v0.5.0: Encoder.EncodeKeyval
已验证示例 — golang github.com/go-logfmt/logfmt v0.5.0: Encoder.EncodeKeyval. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.
sha256:c7be55960872d3f3417f8558b68b7a063b809b8e00f118bd1d3fa7dd888475d8
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
linux 24 · ubuntu · glibc 2.39 x64 go
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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-03 |
案例
HOW- 目标
- verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0
- 符号
-
- github.com/go-logfmt/logfmt.Encoder.EncodeKeyval
- 创建时间
- 2026-09-03T18:59:18Z
契约
- logfmt.Encoder.EncodeKeyval writes the logfmt encoding of a single key and value to the output stream
- logfmt.Encoder.EncodeKeyval writes a single space separator before second and subsequent key-value pairs on the same record
- logfmt.Encoder.EncodeKeyval formats nil values as null
- logfmt.Encoder.EncodeKeyval returns ErrNilKey on nil key or nil pointer and writes nothing to the stream
- logfmt.Encoder.EncodeKeyval returns ErrInvalidKey when key contains only invalid runes and writes nothing to the stream
- logfmt.Encoder.EncodeKeyval returns ErrUnsupportedKeyType for array chan func map slice or struct key and writes nothing to the stream
- logfmt.Encoder.EncodeKeyval returns ErrUnsupportedValueType for unsupported complex types and writes nothing to the stream
文件
- PROMPT.md
- csx.json
- go.mod
- go.sum
- sample.go
- spec.json
- test/contract.go
源代码
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/go-logfmt/logfmt@v0.5.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:golang/github.com/go-logfmt/logfmt@v0.5.0
Demonstrate these symbols/APIs:
- github.com/go-logfmt/logfmt.Encoder.EncodeKeyval
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.
{"case":{"caseId":"case:sha256:abbe8b8081550de2c538216831908649ccb2947490e0f1de52694c076fbd09bc","contract":["logfmt.Encoder.EncodeKeyval writes the logfmt encoding of a single key and value to the output stream","logfmt.Encoder.EncodeKeyval writes a single space separator before second and subsequent key-value pairs on the same record","logfmt.Encoder.EncodeKeyval formats nil values as null","logfmt.Encoder.EncodeKeyval returns ErrNilKey on nil key or nil pointer and writes nothing to the stream","logfmt.Encoder.EncodeKeyval returns ErrInvalidKey when key contains only invalid runes and writes nothing to the stream","logfmt.Encoder.EncodeKeyval returns ErrUnsupportedKeyType for array chan func map slice or struct key and writes nothing to the stream","logfmt.Encoder.EncodeKeyval returns ErrUnsupportedValueType for unsupported complex types and writes nothing to the stream"],"goal":"verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0","kind":"HOW","packages":["pkg:golang/github.com/go-logfmt/logfmt@v0.5.0"],"schemaVersion":1,"symbols":["github.com/go-logfmt/logfmt.Encoder.EncodeKeyval"]},"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/go-logfmt/logfmt@v0.5.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/go-logfmt/logfmt@v0.5.0","symbols":["github.com/go-logfmt/logfmt.Encoder.EncodeKeyval"],"verifierAdapter":"golang@1"}
module sample
go 1.26.6
require github.com/go-logfmt/logfmt v0.5.0
github.com/go-logfmt/logfmt v0.5.0 h1:TrB8swr/68K7m9CcGut2g3UOihhbcbiMAYiuTXdEih4=
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
package sample
import (
"bytes"
"io"
"github.com/go-logfmt/logfmt"
)
// LogEntry writes key-value pairs one by one using EncodeKeyval and ends the record with a newline.
func LogEntry(w io.Writer, kvs [][2]interface{}) error {
enc := logfmt.NewEncoder(w)
for _, kv := range kvs {
if err := enc.EncodeKeyval(kv[0], kv[1]); err != nil {
return err
}
}
return enc.EndRecord()
}
// FormatPair encodes a single key-value pair to a string using EncodeKeyval.
func FormatPair(key, value interface{}) (string, error) {
var buf bytes.Buffer
enc := logfmt.NewEncoder(&buf)
if err := enc.EncodeKeyval(key, value); err != nil {
return "", err
}
return buf.String(), nil
}
{
"schemaVersion": 1,
"goal": "verify pkg:golang/github.com/go-logfmt/logfmt@v0.5.0",
"kind": "HOW",
"packages": [
"pkg:golang/github.com/go-logfmt/logfmt@v0.5.0"
],
"symbols": [
"github.com/go-logfmt/logfmt.Encoder.EncodeKeyval"
]
}
package main
import (
"bytes"
"errors"
"fmt"
"os"
"github.com/go-logfmt/logfmt"
"sample"
)
func main() {
// Assertion 1: logfmt.Encoder.EncodeKeyval writes the logfmt encoding of a single key and value to the output stream
var buf bytes.Buffer
enc := logfmt.NewEncoder(&buf)
if err := enc.EncodeKeyval("msg", "hello world"); err != nil {
fmt.Fprintf(os.Stderr, "Assertion 1 failed: %v\n", err)
os.Exit(1)
}
if buf.String() != "msg=\"hello world\"" {
fmt.Fprintf(os.Stderr, "Assertion 1 output mismatch: %q\n", buf.String())
os.Exit(1)
}
// Assertion 2: logfmt.Encoder.EncodeKeyval writes a single space separator before second and subsequent key-value pairs on the same record
if err := enc.EncodeKeyval("count", 42); err != nil {
fmt.Fprintf(os.Stderr, "Assertion 2 failed: %v\n", err)
os.Exit(1)
}
if buf.String() != "msg=\"hello world\" count=42" {
fmt.Fprintf(os.Stderr, "Assertion 2 output mismatch: %q\n", buf.String())
os.Exit(1)
}
if err := enc.EndRecord(); err != nil {
fmt.Fprintf(os.Stderr, "EndRecord failed: %v\n", err)
os.Exit(1)
}
if buf.String() != "msg=\"hello world\" count=42\n" {
fmt.Fprintf(os.Stderr, "EndRecord output mismatch: %q\n", buf.String())
os.Exit(1)
}
// Assertion 3: logfmt.Encoder.EncodeKeyval formats nil values as null
buf.Reset()
if err := enc.EncodeKeyval("data", nil); err != nil {
fmt.Fprintf(os.Stderr, "Assertion 3 failed: %v\n", err)
os.Exit(1)
}
if buf.String() != "data=null" {
fmt.Fprintf(os.Stderr, "Assertion 3 output mismatch: %q\n", buf.String())
os.Exit(1)
}
// Assertion 4: logfmt.Encoder.EncodeKeyval returns ErrNilKey on nil key or nil pointer and writes nothing to the stream
buf.Reset()
if err := enc.EncodeKeyval(nil, "val"); !errors.Is(err, logfmt.ErrNilKey) {
fmt.Fprintf(os.Stderr, "Assertion 4 nil interface failed: expected ErrNilKey, got %v\n", err)
os.Exit(1)
}
var nilPtr *string
if err := enc.EncodeKeyval(nilPtr, "val"); !errors.Is(err, logfmt.ErrNilKey) {
fmt.Fprintf(os.Stderr, "Assertion 4 nil pointer failed: expected ErrNilKey, got %v\n", err)
os.Exit(1)
}
if buf.Len() != 0 {
fmt.Fprintf(os.Stderr, "Assertion 4 stream mismatch: expected empty buffer, got %q\n", buf.String())
os.Exit(1)
}
// Assertion 5: logfmt.Encoder.EncodeKeyval returns ErrInvalidKey when key contains only invalid runes and writes nothing to the stream
buf.Reset()
if err := enc.EncodeKeyval(" ===", "val"); !errors.Is(err, logfmt.ErrInvalidKey) {
fmt.Fprintf(os.Stderr, "Assertion 5 failed: expected ErrInvalidKey, got %v\n", err)
os.Exit(1)
}
if buf.Len() != 0 {
fmt.Fprintf(os.Stderr, "Assertion 5 stream mismatch: expected empty buffer, got %q\n", buf.String())
os.Exit(1)
}
// Assertion 6: logfmt.Encoder.EncodeKeyval returns ErrUnsupportedKeyType for array chan func map slice or struct key and writes nothing to the stream
buf.Reset()
type dummyKey struct{}
if err := enc.EncodeKeyval(dummyKey{}, "val"); !errors.Is(err, logfmt.ErrUnsupportedKeyType) {
fmt.Fprintf(os.Stderr, "Assertion 6 failed: expected ErrUnsupportedKeyType, got %v\n", err)
os.Exit(1)
}
if buf.Len() != 0 {
fmt.Fprintf(os.Stderr, "Assertion 6 stream mismatch: expected empty buffer, got %q\n", buf.String())
os.Exit(1)
}
// Assertion 7: logfmt.Encoder.EncodeKeyval returns ErrUnsupportedValueType for unsupported complex types and writes nothing to the stream
buf.Reset()
type dummyVal struct{}
if err := enc.EncodeKeyval("meta", dummyVal{}); !errors.Is(err, logfmt.ErrUnsupportedValueType) {
fmt.Fprintf(os.Stderr, "Assertion 7 failed: expected ErrUnsupportedValueType, got %v\n", err)
os.Exit(1)
}
if buf.Len() != 0 {
fmt.Fprintf(os.Stderr, "Assertion 7 stream mismatch: expected empty buffer, got %q\n", buf.String())
os.Exit(1)
}
// Helper verification: LogEntry and FormatPair
pairStr, err := sample.FormatPair("status", "ok")
if err != nil || pairStr != "status=ok" {
fmt.Fprintf(os.Stderr, "FormatPair helper failed: %v, got %q\n", err, pairStr)
os.Exit(1)
}
buf.Reset()
err = sample.LogEntry(&buf, [][2]interface{}{
{"module", "auth"},
{"action", "login"},
})
if err != nil || buf.String() != "module=auth action=login\n" {
fmt.Fprintf(os.Stderr, "LogEntry helper failed: %v, got %q\n", err, buf.String())
os.Exit(1)
}
fmt.Println("All contract assertions passed.")
}
原始种子者
匿名