CodeSampleX

Exemplo

github.com/klauspost/compress v1.20.0: snappy.NewWriter

Amostra verificada para golang github.com/klauspost/compress v1.20.0: snappy.NewWriter. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:906b116b6715b2ed6624afe25459a4d42d93347893c1259e94f66d90ad2c06a6

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado linux 24 · ubuntu · glibc 2.39 x64 go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
verify snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0
Pacotes
Símbolos
  • snappy.NewWriter
Criado
2026-09-03T00:33:55Z

Contrato

  1. snappy.NewWriter returns a non-nil *snappy.Writer wrapping the target io.Writer
  2. snappy.Writer compresses data written through Write method into Snappy framing stream
  3. snappy.Writer.Close flushes and finalizes the compressed stream
  4. snappy.Writer.Reset redirects subsequent compressed writes to a new io.Writer
  5. decompression with snappy.NewReader recovers the exact original bytes written by snappy.NewWriter

Arquivos

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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 snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/klauspost/compress@v1.20.0
Demonstrate these symbols/APIs:
  - snappy.NewWriter

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:70a636d0e648acb736f7d38ad504f87242fb0c58045f0c22306ee5269c32c93b","contract":["snappy.NewWriter returns a non-nil *snappy.Writer wrapping the target io.Writer","snappy.Writer compresses data written through Write method into Snappy framing stream","snappy.Writer.Close flushes and finalizes the compressed stream","snappy.Writer.Reset redirects subsequent compressed writes to a new io.Writer","decompression with snappy.NewReader recovers the exact original bytes written by snappy.NewWriter"],"goal":"verify snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0","kind":"HOW","packages":["pkg:golang/github.com/klauspost/compress@v1.20.0"],"schemaVersion":1,"symbols":["snappy.NewWriter"]},"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/klauspost/compress@v1.20.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/compress@v1.20.0","symbols":["snappy.NewWriter"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/klauspost/compress v1.20.0
go.sum
github.com/klauspost/compress v1.20.0 h1:a3C1ke2ohxFymNlb2HWAHjDeKCI90scRskErZkR0ezA=
github.com/klauspost/compress v1.20.0/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/klauspost/compress/snappy"
)

func main() {
	var buf bytes.Buffer
	w := snappy.NewWriter(&buf)

	data := []byte("Streaming compression with klauspost/compress snappy.NewWriter")
	if _, err := w.Write(data); err != nil {
		panic(err)
	}

	if err := w.Close(); err != nil {
		panic(err)
	}

	compressedLen := buf.Len()
	r := snappy.NewReader(&buf)
	decompressed, err := io.ReadAll(r)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Compressed %d bytes down to %d bytes\n", len(data), compressedLen)
	fmt.Printf("Decompressed string: %s\n", string(decompressed))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/compress@v1.20.0"
  ],
  "symbols": [
    "snappy.NewWriter"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	"github.com/klauspost/compress/snappy"
)

func main() {
	// 1. snappy.NewWriter returns a non-nil *snappy.Writer wrapping the target io.Writer
	var buf bytes.Buffer
	w := snappy.NewWriter(&buf)
	if w == nil {
		fmt.Fprintln(os.Stderr, "FAIL: snappy.NewWriter returned nil")
		os.Exit(1)
	}

	// 2. snappy.Writer compresses data written through Write method into Snappy framing stream
	input1 := []byte("CodeSampleX contract verification: snappy.Writer streaming compression.")
	n, err := w.Write(input1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write failed: %v\n", err)
		os.Exit(1)
	}
	if n != len(input1) {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write short write: got %d want %d\n", n, len(input1))
		os.Exit(1)
	}

	// 3. snappy.Writer.Close flushes and finalizes the compressed stream
	if err := w.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Close failed: %v\n", err)
		os.Exit(1)
	}
	compressed1 := buf.Bytes()
	if len(compressed1) == 0 {
		fmt.Fprintln(os.Stderr, "FAIL: compressed stream is empty after Close")
		os.Exit(1)
	}
	snappyMagic := []byte("\xff\x06\x00\x00sNaPpY")
	if !bytes.HasPrefix(compressed1, snappyMagic) {
		fmt.Fprintf(os.Stderr, "FAIL: compressed stream missing Snappy magic header\n")
		os.Exit(1)
	}

	// 4. snappy.Writer.Reset redirects subsequent compressed writes to a new io.Writer
	var buf2 bytes.Buffer
	w.Reset(&buf2)
	input2 := []byte("Second stream payload following snappy.Writer.Reset call.")
	n2, err := w.Write(input2)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write after Reset failed: %v\n", err)
		os.Exit(1)
	}
	if n2 != len(input2) {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write after Reset short write: got %d want %d\n", n2, len(input2))
		os.Exit(1)
	}
	if err := w.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Close after Reset failed: %v\n", err)
		os.Exit(1)
	}
	compressed2 := buf2.Bytes()
	if !bytes.HasPrefix(compressed2, snappyMagic) {
		fmt.Fprintf(os.Stderr, "FAIL: second stream missing Snappy magic header\n")
		os.Exit(1)
	}

	// 5. decompression with snappy.NewReader recovers the exact original bytes written by snappy.NewWriter
	r1 := snappy.NewReader(bytes.NewReader(compressed1))
	decomp1, err := io.ReadAll(r1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ReadAll on stream 1 failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(decomp1, input1) {
		fmt.Fprintf(os.Stderr, "FAIL: stream 1 mismatch: got %q want %q\n", decomp1, input1)
		os.Exit(1)
	}

	r2 := snappy.NewReader(bytes.NewReader(compressed2))
	decomp2, err := io.ReadAll(r2)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ReadAll on stream 2 failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(decomp2, input2) {
		fmt.Fprintf(os.Stderr, "FAIL: stream 2 mismatch: got %q want %q\n", decomp2, input2)
		os.Exit(1)
	}

	fmt.Println("All contract assertions passed successfully.")
}

Seeder de origem

anônimo