CodeSampleX

Exemplo

golang.org/x/sync v0.22.0: syncmap.Map

Amostra verificada para golang golang.org/x/sync v0.22.0: syncmap.Map. O contrato rodou em go 1.26 · linux alpine/x64 · docker e passou.

sha256:790af6712925de14871c37b6dff75594fa6d129fee8ec89b8da639a4bd86fc09

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 alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-27

Caso

HOW
Objetivo
verify golang.org/x/sync/syncmap.Map in pkg:golang/golang.org/x/sync@v0.22.0
Pacotes
Símbolos
  • golang.org/x/sync/syncmap.Map
Criado
2026-08-27T22:02:28Z

Contrato

  1. syncmap.Map Store stores key-value pair and Load retrieves it
  2. syncmap.Map Load on nonexistent key returns ok=false
  3. syncmap.Map LoadOrStore returns existing value if present without modifying
  4. syncmap.Map LoadOrStore stores and returns new value when key is absent
  5. syncmap.Map LoadAndDelete deletes key and returns previous value and loaded=true
  6. syncmap.Map Delete removes key from map
  7. syncmap.Map Swap replaces value and returns previous value
  8. syncmap.Map CompareAndSwap conditionally replaces value when old matches
  9. syncmap.Map CompareAndDelete conditionally deletes key when old matches
  10. syncmap.Map Range iterates over entries and stops when callback returns false
  11. syncmap.Map safely handles concurrent reads and writes from multiple goroutines

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 golang.org/x/sync/syncmap.Map in pkg:golang/golang.org/x/sync@v0.22.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/sync@v0.22.0
Demonstrate these symbols/APIs:
  - golang.org/x/sync/syncmap.Map

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:bfe72d6d8164687b2e49dea302c46696ee259baf497617b8aeaa93c283fb0385","contract":["syncmap.Map Store stores key-value pair and Load retrieves it","syncmap.Map Load on nonexistent key returns ok=false","syncmap.Map LoadOrStore returns existing value if present without modifying","syncmap.Map LoadOrStore stores and returns new value when key is absent","syncmap.Map LoadAndDelete deletes key and returns previous value and loaded=true","syncmap.Map Delete removes key from map","syncmap.Map Swap replaces value and returns previous value","syncmap.Map CompareAndSwap conditionally replaces value when old matches","syncmap.Map CompareAndDelete conditionally deletes key when old matches","syncmap.Map Range iterates over entries and stops when callback returns false","syncmap.Map safely handles concurrent reads and writes from multiple goroutines"],"goal":"verify golang.org/x/sync/syncmap.Map in pkg:golang/golang.org/x/sync@v0.22.0","kind":"HOW","packages":["pkg:golang/golang.org/x/sync@v0.22.0"],"schemaVersion":1,"symbols":["golang.org/x/sync/syncmap.Map"]},"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/golang.org/x/sync@v0.22.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/sync@v0.22.0","symbols":["golang.org/x/sync/syncmap.Map"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.25.0

require golang.org/x/sync v0.22.0
go.sum
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
main.go
package main

import (
	"fmt"

	"golang.org/x/sync/syncmap"
)

func main() {
	var m syncmap.Map

	m.Store("greeting", "hello world")
	if val, ok := m.Load("greeting"); ok {
		fmt.Println(val)
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/sync/syncmap.Map in pkg:golang/golang.org/x/sync@v0.22.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/sync@v0.22.0"
  ],
  "symbols": [
    "golang.org/x/sync/syncmap.Map"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"sync"

	"golang.org/x/sync/syncmap"
)

func assert(cond bool, msg string) {
	if !cond {
		panic("assertion failed: " + msg)
	}
}

func main() {
	// Test 1: Store and Load
	{
		var m syncmap.Map
		m.Store("key1", "val1")
		val, ok := m.Load("key1")
		assert(ok && val == "val1", "Store and Load should return stored value")

		_, ok = m.Load("nonexistent")
		assert(!ok, "Load on nonexistent key should return ok=false")
	}

	// Test 2: LoadOrStore
	{
		var m syncmap.Map
		actual, loaded := m.LoadOrStore("key1", "val1")
		assert(!loaded && actual == "val1", "LoadOrStore should insert new entry when key is missing")

		actual, loaded = m.LoadOrStore("key1", "val2")
		assert(loaded && actual == "val1", "LoadOrStore should return existing entry without modifying")
	}

	// Test 3: LoadAndDelete and Delete
	{
		var m syncmap.Map
		m.Store("key1", "val1")
		val, loaded := m.LoadAndDelete("key1")
		assert(loaded && val == "val1", "LoadAndDelete should return previous value and loaded=true")

		_, loaded = m.LoadAndDelete("key1")
		assert(!loaded, "LoadAndDelete on missing key should return loaded=false")

		m.Store("key2", "val2")
		m.Delete("key2")
		_, ok := m.Load("key2")
		assert(!ok, "Delete should remove entry")
	}

	// Test 4: Swap
	{
		var m syncmap.Map
		prev, loaded := m.Swap("key1", "val1")
		assert(!loaded && prev == nil, "Swap on new key should return loaded=false and prev=nil")

		prev, loaded = m.Swap("key1", "val2")
		assert(loaded && prev == "val1", "Swap on existing key should return previous value")

		val, ok := m.Load("key1")
		assert(ok && val == "val2", "Load should reflect swapped value")
	}

	// Test 5: CompareAndSwap
	{
		var m syncmap.Map
		m.Store("key1", "val1")
		swapped := m.CompareAndSwap("key1", "wrong", "val2")
		assert(!swapped, "CompareAndSwap with mismatched old value should return false")

		val, ok := m.Load("key1")
		assert(ok && val == "val1", "CompareAndSwap failure should leave value untouched")

		swapped = m.CompareAndSwap("key1", "val1", "val2")
		assert(swapped, "CompareAndSwap with matching old value should return true")

		val, ok = m.Load("key1")
		assert(ok && val == "val2", "CompareAndSwap success should update value")
	}

	// Test 6: CompareAndDelete
	{
		var m syncmap.Map
		m.Store("key1", "val1")
		deleted := m.CompareAndDelete("key1", "wrong")
		assert(!deleted, "CompareAndDelete with mismatched old value should return false")

		val, ok := m.Load("key1")
		assert(ok && val == "val1", "CompareAndDelete failure should leave key intact")

		deleted = m.CompareAndDelete("key1", "val1")
		assert(deleted, "CompareAndDelete with matching old value should return true")

		_, ok = m.Load("key1")
		assert(!ok, "CompareAndDelete success should remove key")
	}

	// Test 7: Range
	{
		var m syncmap.Map
		m.Store("k1", 1)
		m.Store("k2", 2)
		m.Store("k3", 3)

		visited := make(map[string]int)
		m.Range(func(k, v any) bool {
			visited[k.(string)] = v.(int)
			return true
		})
		assert(len(visited) == 3 && visited["k1"] == 1 && visited["k2"] == 2 && visited["k3"] == 3, "Range should visit all entries")

		count := 0
		m.Range(func(k, v any) bool {
			count++
			return false // stop early
		})
		assert(count == 1, "Range should stop when false is returned")
	}

	// Test 8: Concurrent read/write safety
	{
		var m syncmap.Map
		var wg sync.WaitGroup
		const numGoroutines = 50
		const iters = 100

		for i := 0; i < numGoroutines; i++ {
			wg.Add(1)
			go func(gid int) {
				defer wg.Done()
				for j := 0; j < iters; j++ {
					key := fmt.Sprintf("k-%d", (gid+j)%10)
					m.Store(key, j)
					m.Load(key)
					m.LoadOrStore(key, j)
					if j%2 == 0 {
						m.Delete(key)
					}
				}
			}(i)
		}
		wg.Wait()
	}

	fmt.Println("Contract verification passed successfully")
}

Seeder de origem

anônimo