CodeSampleX

サンプル

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

検証済みサンプル — golang golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa: mmap.Open, mmap.ReaderAt. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:178513401122ca8df461325adc547f87f6b1a723257aad9a8440da0a5b372569

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go 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/golang.org/x/exp@v0.0.0-20260824195058-e88cd73687aa
パッケージ
シンボル
  • golang.org/x/exp/mmap.Open
  • golang.org/x/exp/mmap.ReaderAt
作成日
2026-09-03T23:27:27Z

コントラクト

  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

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • mmapsample.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/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")
}

オリジンシーダー

匿名