CodeSampleX

示例

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

已验证示例 — golang golang.org/x/sync v0.22.0: syncmap.Map. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: syncmap.Map Store stores key-value pair and…

sha256:790af6712925de14871c37b6dff75594fa6d129fee8ec89b8da639a4bd86fc09

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify golang.org/x/sync/syncmap.Map in pkg:golang/golang.org/x/sync@v0.22.0
包
符号
  • golang.org/x/sync/syncmap.Map
创建时间
2026-08-27T22:02:28Z

契约

  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

文件

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

原始种子者

匿名