CodeSampleX

Exemplo

golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa: mmap.Open, mmap.ReaderAt

Amostra verificada para golang golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa: mmap.Open, mmap.ReaderAt. O contrato rodou em go 1.26 · linux debian/x64…

sha256:178513401122ca8df461325adc547f87f6b1a723257aad9a8440da0a5b372569

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 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 pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa
Pacotes
Símbolos
  • golang.org/x/exp/mmap.Open
  • golang.org/x/exp/mmap.ReaderAt
Criado
2026-09-03T23:27:27Z

Contrato

  1. mmap.Open memory-maps a file for read-only access
  2. mmap.ReaderAt.Len reports the byte length of the underlying mapped file
  3. mmap.ReaderAt.At accesses individual bytes by index without copying
  4. mmap.ReaderAt.ReadAt reads byte slices at arbitrary offsets and returns io.EOF at end of file
  5. mmap.ReaderAt.Close unmaps the memory region and causes subsequent reads to fail

Arquivos

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • mmapsample.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 pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa
Demonstrate these symbols/APIs:
  - golang.org/x/exp/mmap.Open
  - golang.org/x/exp/mmap.ReaderAt

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:df25901db88884777e25bae6186bd512791ff02b3edef3bc86eb6dc175e0a390","contract":["mmap.Open memory-maps a file for read-only access","mmap.ReaderAt.Len reports the byte length of the underlying mapped file","mmap.ReaderAt.At accesses individual bytes by index without copying","mmap.ReaderAt.ReadAt reads byte slices at arbitrary offsets and returns io.EOF at end of file","mmap.ReaderAt.Close unmaps the memory region and causes subsequent reads to fail"],"goal":"verify pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa","kind":"HOW","packages":["pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa"],"schemaVersion":1,"symbols":["golang.org/x/exp/mmap.Open","golang.org/x/exp/mmap.ReaderAt"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa","symbols":["golang.org/x/exp/mmap.Open","golang.org/x/exp/mmap.ReaderAt"],"verifierAdapter":"golang@1"}
go.mod
module example.com/mmapsample

go 1.26.0

require golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa
go.sum
golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa h1:QSyA8ishJCyT21kER9KwNt0b7BM3iRK4x9QXhjN5Fdk=
golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa/go.mod h1:zeBbvyFKDaLwa7CH/zI8KXt7gTl14SF7sO08Pl5jBCM=
mmapsample.go
package mmapsample

import (
	"golang.org/x/exp/mmap"
)

// OpenFile opens and memory-maps the named file for reading.
func OpenFile(filename string) (*mmap.ReaderAt, error) {
	return mmap.Open(filename)
}

// ReadChunk reads a byte chunk of the given length at offset from the memory-mapped reader.
func ReadChunk(r *mmap.ReaderAt, offset int64, length int) ([]byte, error) {
	buf := make([]byte, length)
	n, err := r.ReadAt(buf, offset)
	return buf[:n], err
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa"
  ],
  "symbols": [
    "golang.org/x/exp/mmap.Open",
    "golang.org/x/exp/mmap.ReaderAt"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"path/filepath"

	"example.com/mmapsample"
	"golang.org/x/exp/mmap"
)

func main() {
	tmpDir, err := os.MkdirTemp("", "mmap-contract-*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: failed to create temp dir: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	filePath := filepath.Join(tmpDir, "sample.txt")
	testData := []byte("The quick brown fox jumps over the lazy dog. CodeSampleX mmap verification!")
	if err := os.WriteFile(filePath, testData, 0600); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: failed to write test file: %v\n", err)
		os.Exit(1)
	}

	// 1. mmap.Open memory-maps a file for read-only access
	var reader *mmap.ReaderAt
	reader, err = mmap.Open(filePath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: mmap.Open failed: %v\n", err)
		os.Exit(1)
	}
	defer reader.Close()

	// 2. mmap.ReaderAt.Len reports the byte length of the underlying mapped file
	if reader.Len() != len(testData) {
		fmt.Fprintf(os.Stderr, "FAIL: ReaderAt.Len() = %d, want %d\n", reader.Len(), len(testData))
		os.Exit(1)
	}

	// 3. mmap.ReaderAt.At accesses individual bytes by index without copying
	if reader.At(0) != 'T' || reader.At(4) != 'q' || reader.At(len(testData)-1) != '!' {
		fmt.Fprintf(os.Stderr, "FAIL: ReaderAt.At returned unexpected bytes\n")
		os.Exit(1)
	}

	// 4. mmap.ReaderAt.ReadAt reads byte slices at arbitrary offsets and returns io.EOF at end of file
	midBuf, err := mmapsample.ReadChunk(reader, 10, 5) // "brown"
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ReadChunk mid failed: %v\n", err)
		os.Exit(1)
	}
	if string(midBuf) != "brown" {
		fmt.Fprintf(os.Stderr, "FAIL: ReadChunk mid mismatch: got %q, want %q\n", string(midBuf), "brown")
		os.Exit(1)
	}

	endBuf := make([]byte, 30)
	offset := int64(len(testData) - 13) // "verification!"
	n, err := reader.ReadAt(endBuf, offset)
	if err != io.EOF {
		fmt.Fprintf(os.Stderr, "FAIL: ReadAt at end expected io.EOF, got %v\n", err)
		os.Exit(1)
	}
	if n != 13 || !bytes.Equal(endBuf[:n], []byte("verification!")) {
		fmt.Fprintf(os.Stderr, "FAIL: ReadAt at end mismatch: got %q (len %d), want %q\n", string(endBuf[:n]), n, "verification!")
		os.Exit(1)
	}

	// 5. mmap.ReaderAt.Close unmaps the memory region and causes subsequent reads to fail
	if err := reader.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ReaderAt.Close failed: %v\n", err)
		os.Exit(1)
	}
	buf := make([]byte, 5)
	_, err = reader.ReadAt(buf, 0)
	if err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: ReadAt succeeded after Close\n")
		os.Exit(1)
	}

	// Idempotent close
	if err := reader.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: second Close returned error: %v\n", err)
		os.Exit(1)
	}

	fmt.Println("PASS: golang.org/x/exp/mmap contract verified")
}

Seeder de origem

anônimo