func run(tasks []func() error) error {
errs := make(chan error) // unbuffered
var wg sync.WaitGroup
for _, task := range tasks {
wg.Go(func() { errs <- task() }) // (1)
}
for range tasks {
if err := <-errs; err != nil {
return err // (2)
}
}
wg.Wait()
return nil
}func replicate(replicas []func() string) string {
results := make(chan string) // unbuffered
for _, r := range replicas {
go func() { results <- r() }() // (1)
}
return <-results // (2)
}func stream(work []int) {
out := make(chan int)
go func() {
for v := range out { // (1)
handle(v)
}
}()
for _, v := range work {
out <- v
}
// (2) no close(out)
}func main() {
f, _ := os.Create("leak.pb.gz")
pprof.Lookup("goroutineleak").WriteTo(f, 0)
}// kill -USR1 <pid> to dump leaks
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGUSR1)
go func() {
for range sig {
pprof.Lookup("goroutineleak").WriteTo(os.Stdout, 1)
}
}()goroutineleak profile: total 2
1 @ ...
# 0x... main.leakSend.func1+0x27 formats/main.go:15
1 @ ...
# 0x... main.leakRange.func1+0x33 formats/main.go:21
goroutine 7 [chan send (leaked)]:
main.leakSend.func1()
formats/main.go:15 +0x28
created by main.leakSend in goroutine 1
formats/main.go:15 +0x6c
goroutine 8 [chan receive (leaked)]:
main.leakRange.func1()
formats/main.go:21 +0x34
created by main.leakRange in goroutine 1
formats/main.go:20 +0x6cfunc leaked() (string, bool) {
p := pprof.Lookup("goroutineleak")
if p == nil {
return "", false // experiment off, nothing to detect
}
var b bytes.Buffer
p.WriteTo(&b, 1)
return b.String(), p.Count() > 0
}// verifyNone mirrors goleak.VerifyNone.
func verifyNone(t *testing.T) {
t.Helper()
if report, ok := leaked(); ok {
t.Fatalf("leaked goroutines:\n%s", report)
}
}
func TestRun(t *testing.T) {
defer verifyNone(t)
// ... exercise the code under test ...
}// verifyTestMain mirrors goleak.VerifyTestMain.
func verifyTestMain(m *testing.M) {
code := m.Run()
if code == 0 {
if report, ok := leaked(); ok {
fmt.Fprintf(os.Stderr, "leaked goroutines:\n%s", report)
code = 1
}
}
os.Exit(code)
}
func TestMain(m *testing.M) {
verifyTestMain(m)
}// registers /debug/pprof/goroutineleak on http.DefaultServeMux
import _ "net/http/pprof"
http.ListenAndServe("localhost:6060", nil)$ curl 'localhost:6060/debug/pprof/goroutineleak?debug=1'
goroutineleak profile: total 1
1 @ ...
# 0x... main.main.func1+0x27 server/main.go:14$ go tool pprof -top 'http://localhost:6060/debug/pprof/goroutineleak'
Type: goroutineleak
flat flat% sum% cum cum%
1 100% 100% 1 100% runtime.gopark
0 0% 100% 1 100% main.main.func1
0 0% 100% 1 100% runtime.chansend
0 0% 100% 1 100% runtime.chansend1$ GOEXPERIMENT=jsonv2,goroutineleakprofile go build ./...$ GOEXPERIMENT=nogreenteagc,norandomizedheapbase64 go build ./...
$ GOEXPERIMENT=jsonv2,nogreenteagc go build ./...package main
import (
"encoding/json/v2"
"fmt"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
City string `json:"city"`
}
func main() {
p := Person{Name: "Ada", Age: 36, City: "Vienna"}
data, _ := json.Marshal(p, json.StringifyNumbers(true))
fmt.Println(string(data))
}$ go run main.go
package command-line-arguments
imports encoding/json/v2: build constraints exclude all Go files in /usr/local/go/src/encoding/json/v2$ GOEXPERIMENT=jsonv2 go run main.go
{"name":"Ada","age":"36","city":"Vienna"}Время выполнения popcount в bitmap-контейнере
Время создания array-контейнера из плотной битовой карты
Время определения мощности пересечения
go tool pprof BleveIndexBatch100-1212217505-cpu.prof
File: bleve-index-bench
Build ID: 81f203eb5714d9755a42c833024d5f60afd94e03
Type: cpu
Time: 2026-03-10 16:10:56 IST
Duration: 5.40s, Total samples = 15.18s (280.91%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) top 10
Showing nodes accounting for 8830ms, 58.17% of 15180ms total
Dropped 208 nodes (cum <= 75.90ms)
Showing top 10 nodes out of 103
flat flat% sum% cum cum%
3170ms 20.88% 20.88% 4260ms 28.06% runtime.tryDeferToSpanScan
1100ms 7.25% 28.13% 2970ms 19.57% runtime.scanObjectSmall
840ms 5.53% 33.66% 900ms 5.93% github.com/blevesearch/segment.segmentWords
710ms 4.68% 38.34% 7180ms 47.30% runtime.scanSpan
670ms 4.41% 42.75% 2940ms 19.37% runtime.scanObjectsSmall
520ms 3.43% 46.18% 970ms 6.39% github.com/blevesearch/bleve/index/store/gtreap.itemCompare
520ms 3.43% 49.60% 3610ms 23.78% github.com/steveyen/gtreap.(*Treap).union
440ms 2.90% 52.50% 2070ms 13.64% github.com/steveyen/gtreap.(*Treap).split
430ms 2.83% 55.34% 450ms 2.96% runtime.extractHeapBitsSmall
430ms 2.83% 58.17% 1690ms 11.13% runtime.mallocgcSmallScanNoHeader