File size: 25,058 Bytes
545ed55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
# Golang - sync

Source: https://pkg.go.dev/sync@go1.20
Version: go1.20

## Documentation [](#section-documentation "Go to Documentation")

### Overview [](#pkg-overview "Go to Overview")

Package sync provides basic synchronization primitives such as mutual
exclusion locks. Other than the Once and WaitGroup types, most are intended
for use by low-level library routines. Higher-level synchronization is
better done via channels and communication.

Values containing the types defined in this package should not be copied.

### Index [](#pkg-index "Go to Index")

* [type Cond](#Cond)
* + [func NewCond(l Locker) \*Cond](#NewCond)
* + [func (c \*Cond) Broadcast()](#Cond.Broadcast)
  + [func (c \*Cond) Signal()](#Cond.Signal)
  + [func (c \*Cond) Wait()](#Cond.Wait)
* [type Locker](#Locker)
* [type Map](#Map)
* + [func (m \*Map) CompareAndDelete(key, old any) (deleted bool)](#Map.CompareAndDelete)
  + [func (m \*Map) CompareAndSwap(key, old, new any) bool](#Map.CompareAndSwap)
  + [func (m \*Map) Delete(key any)](#Map.Delete)
  + [func (m \*Map) Load(key any) (value any, ok bool)](#Map.Load)
  + [func (m \*Map) LoadAndDelete(key any) (value any, loaded bool)](#Map.LoadAndDelete)
  + [func (m \*Map) LoadOrStore(key, value any) (actual any, loaded bool)](#Map.LoadOrStore)
  + [func (m \*Map) Range(f func(key, value any) bool)](#Map.Range)
  + [func (m \*Map) Store(key, value any)](#Map.Store)
  + [func (m \*Map) Swap(key, value any) (previous any, loaded bool)](#Map.Swap)
* [type Mutex](#Mutex)
* + [func (m \*Mutex) Lock()](#Mutex.Lock)
  + [func (m \*Mutex) TryLock() bool](#Mutex.TryLock)
  + [func (m \*Mutex) Unlock()](#Mutex.Unlock)
* [type Once](#Once)
* + [func (o \*Once) Do(f func())](#Once.Do)
* [type Pool](#Pool)
* + [func (p \*Pool) Get() any](#Pool.Get)
  + [func (p \*Pool) Put(x any)](#Pool.Put)
* [type RWMutex](#RWMutex)
* + [func (rw \*RWMutex) Lock()](#RWMutex.Lock)
  + [func (rw \*RWMutex) RLock()](#RWMutex.RLock)
  + [func (rw \*RWMutex) RLocker() Locker](#RWMutex.RLocker)
  + [func (rw \*RWMutex) RUnlock()](#RWMutex.RUnlock)
  + [func (rw \*RWMutex) TryLock() bool](#RWMutex.TryLock)
  + [func (rw \*RWMutex) TryRLock() bool](#RWMutex.TryRLock)
  + [func (rw \*RWMutex) Unlock()](#RWMutex.Unlock)
* [type WaitGroup](#WaitGroup)
* + [func (wg \*WaitGroup) Add(delta int)](#WaitGroup.Add)
  + [func (wg \*WaitGroup) Done()](#WaitGroup.Done)
  + [func (wg \*WaitGroup) Wait()](#WaitGroup.Wait)

### Examples [](#pkg-examples "Go to Examples")

* [Once](#example-Once)
* [Pool](#example-Pool)
* [WaitGroup](#example-WaitGroup)

### Constants [](#pkg-constants "Go to Constants")

This section is empty.

### Variables [](#pkg-variables "Go to Variables")

This section is empty.

### Functions [](#pkg-functions "Go to Functions")

This section is empty.

### Types [](#pkg-types "Go to Types")

#### type [Cond](https://cs.opensource.google/go/go/+/go1.20:src/sync/cond.go;l=36) [](#Cond "Go to Cond")

```None
type Cond struct {

	// L is held while observing or changing the condition
	L Locker
	// contains filtered or unexported fields
}
```

Cond implements a condition variable, a rendezvous point
for goroutines waiting for or announcing the occurrence
of an event.

Each Cond has an associated Locker L (often a \*Mutex or \*RWMutex),
which must be held when changing the condition and
when calling the Wait method.

A Cond must not be copied after first use.

In the terminology of the Go memory model, Cond arranges that
a call to Broadcast or Signal “synchronizes before” any Wait call
that it unblocks.

For many simple use cases, users will be better off using channels than a
Cond (Broadcast corresponds to closing a channel, and Signal corresponds to
sending on a channel).

For more on replacements for sync.Cond, see [Roberto Clapis's series on
advanced concurrency patterns](https://blogtitle.github.io/categories/concurrency/), as well as [Bryan Mills's talk on concurrency
patterns](https://drive.google.com/file/d/1nPdvhB0PutEJzdCq5ms6UI58dp50fcAN/view).

#### func [NewCond](https://cs.opensource.google/go/go/+/go1.20:src/sync/cond.go;l=47) [](#NewCond "Go to NewCond")

```None
func NewCond(l Locker) *Cond
```

NewCond returns a new Cond with Locker l.

#### func (\*Cond) [Broadcast](https://cs.opensource.google/go/go/+/go1.20:src/sync/cond.go;l=90) [](#Cond.Broadcast "Go to Cond.Broadcast")

```None
func (c *Cond) Broadcast()
```

Broadcast wakes all goroutines waiting on c.

It is allowed but not required for the caller to hold c.L
during the call.

#### func (\*Cond) [Signal](https://cs.opensource.google/go/go/+/go1.20:src/sync/cond.go;l=81) [¶](#Cond.Signal "Go to Cond.Signal")

```None
func (c *Cond) Signal()
```

Signal wakes one goroutine waiting on c, if there is any.

It is allowed but not required for the caller to hold c.L
during the call.

Signal() does not affect goroutine scheduling priority; if other goroutines
are attempting to lock c.L, they may be awoken before a "waiting" goroutine.

#### func (\*Cond) [Wait](https://cs.opensource.google/go/go/+/go1.20:src/sync/cond.go;l=66) [](#Cond.Wait "Go to Cond.Wait")

```None
func (c *Cond) Wait()
```

Wait atomically unlocks c.L and suspends execution
of the calling goroutine. After later resuming execution,
Wait locks c.L before returning. Unlike in other systems,
Wait cannot return unless awoken by Broadcast or Signal.

Because c.L is not locked while Wait is waiting, the caller
typically cannot assume that the condition is true when
Wait returns. Instead, the caller should Wait in a loop:

```None
c.L.Lock()
for !condition() {
    c.Wait()
}
... make use of condition ...
c.L.Unlock()
```

#### type [Locker](https://cs.opensource.google/go/go/+/go1.20:src/sync/mutex.go;l=40) [](#Locker "Go to Locker")

```None
type Locker interface {
	Lock()
	Unlock()
}
```

A Locker represents an object that can be locked and unlocked.

#### type [Map](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=35) [](#Map "Go to Map") added in go1.9

```None
type Map struct {
	// contains filtered or unexported fields
}
```

Map is like a Go map[interface{}]interface{} but is safe for concurrent use
by multiple goroutines without additional locking or coordination.
Loads, stores, and deletes run in amortized constant time.

The Map type is specialized. Most code should use a plain Go map instead,
with separate locking or coordination, for better type safety and to make it
easier to maintain other invariants along with the map content.

The Map type is optimized for two common use cases: (1) when the entry for a given
key is only ever written once but read many times, as in caches that only grow,
or (2) when multiple goroutines read, write, and overwrite entries for disjoint
sets of keys. In these two cases, use of a Map may significantly reduce lock
contention compared to a Go map paired with a separate Mutex or RWMutex.

The zero Map is empty and ready for use. A Map must not be copied after first use.

In the terminology of the Go memory model, Map arranges that a write operation
“synchronizes before” any read operation that observes the effect of the write, where
read and write operations are defined as follows.
Load, LoadAndDelete, LoadOrStore, Swap, CompareAndSwap, and CompareAndDelete
are read operations; Delete, LoadAndDelete, Store, and Swap are write operations;
LoadOrStore is a write operation when it returns loaded set to false;
CompareAndSwap is a write operation when it returns swapped set to true;
and CompareAndDelete is a write operation when it returns deleted set to true.

#### func (\*Map) [CompareAndDelete](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=406) [](#Map.CompareAndDelete "Go to Map.CompareAndDelete") added in go1.20

```None
func (m *Map) CompareAndDelete(key, old any) (deleted bool)
```

CompareAndDelete deletes the entry for key if its value is equal to old.
The old value must be of a comparable type.

If there is no current value for key in the map, CompareAndDelete
returns false (even if the old value is the nil interface value).

#### func (\*Map) [CompareAndSwap](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=374) [¶](#Map.CompareAndSwap "Go to Map.CompareAndSwap") added in go1.20

```None
func (m *Map) CompareAndSwap(key, old, new any) bool
```

CompareAndSwap swaps the old and new values for key
if the value stored in the map is equal to old.
The old value must be of a comparable type.

#### func (\*Map) [Delete](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=296) [](#Map.Delete "Go to Map.Delete") added in go1.9

```None
func (m *Map) Delete(key any)
```

Delete deletes the value for a key.

#### func (\*Map) [Load](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=120) [](#Map.Load "Go to Map.Load") added in go1.9

```None
func (m *Map) Load(key any) (value any, ok bool)
```

Load returns the value stored in the map for a key, or nil if no
value is present.
The ok result indicates whether value was found in the map.

#### func (\*Map) [LoadAndDelete](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=272) [¶](#Map.LoadAndDelete "Go to Map.LoadAndDelete") added in go1.15

```None
func (m *Map) LoadAndDelete(key any) (value any, loaded bool)
```

LoadAndDelete deletes the value for a key, returning the previous value if any.
The loaded result reports whether the key was present.

#### func (\*Map) [LoadOrStore](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=203) [](#Map.LoadOrStore "Go to Map.LoadOrStore") added in go1.9

```None
func (m *Map) LoadOrStore(key, value any) (actual any, loaded bool)
```

LoadOrStore returns the existing value for the key if present.
Otherwise, it stores and returns the given value.
The loaded result is true if the value was loaded, false if stored.

#### func (\*Map) [Range](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=449) [](#Map.Range "Go to Map.Range") added in go1.9

```None
func (m *Map) Range(f func(key, value any) bool)
```

Range calls f sequentially for each key and value present in the map.
If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's
contents: no key will be visited more than once, but if the value for any key
is stored or deleted concurrently (including by f), Range may reflect any
mapping for that key from any point during the Range call. Range does not
block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns
false after a constant number of calls.

#### func (\*Map) [Store](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=154) [¶](#Map.Store "Go to Map.Store") added in go1.9

```None
func (m *Map) Store(key, value any)
```

Store sets the value for a key.

#### func (\*Map) [Swap](https://cs.opensource.google/go/go/+/go1.20:src/sync/map.go;l=330) [](#Map.Swap "Go to Map.Swap") added in go1.20

```None
func (m *Map) Swap(key, value any) (previous any, loaded bool)
```

Swap swaps the value for a key and returns the previous value if any.
The loaded result reports whether the key was present.

#### type [Mutex](https://cs.opensource.google/go/go/+/go1.20:src/sync/mutex.go;l=34) [](#Mutex "Go to Mutex")

```None
type Mutex struct {
	// contains filtered or unexported fields
}
```

A Mutex is a mutual exclusion lock.
The zero value for a Mutex is an unlocked mutex.

A Mutex must not be copied after first use.

In the terminology of the Go memory model,
the n'th call to Unlock “synchronizes before” the m'th call to Lock
for any n < m.
A successful call to TryLock is equivalent to a call to Lock.
A failed call to TryLock does not establish any “synchronizes before”
relation at all.

#### func (\*Mutex) [Lock](https://cs.opensource.google/go/go/+/go1.20:src/sync/mutex.go;l=81) [](#Mutex.Lock "Go to Mutex.Lock")

```None
func (m *Mutex) Lock()
```

Lock locks m.
If the lock is already in use, the calling goroutine
blocks until the mutex is available.

#### func (\*Mutex) [TryLock](https://cs.opensource.google/go/go/+/go1.20:src/sync/mutex.go;l=98) [¶](#Mutex.TryLock "Go to Mutex.TryLock") added in go1.18

```None
func (m *Mutex) TryLock() bool
```

TryLock tries to lock m and reports whether it succeeded.

Note that while correct uses of TryLock do exist, they are rare,
and use of TryLock is often a sign of a deeper problem
in a particular use of mutexes.

#### func (\*Mutex) [Unlock](https://cs.opensource.google/go/go/+/go1.20:src/sync/mutex.go;l=212) [](#Mutex.Unlock "Go to Mutex.Unlock")

```None
func (m *Mutex) Unlock()
```

Unlock unlocks m.
It is a run-time error if m is not locked on entry to Unlock.

A locked Mutex is not associated with a particular goroutine.
It is allowed for one goroutine to lock a Mutex and then
arrange for another goroutine to unlock it.

#### type [Once](https://cs.opensource.google/go/go/+/go1.20:src/sync/once.go;l=18) [](#Once "Go to Once")

```None
type Once struct {
	// contains filtered or unexported fields
}
```

Once is an object that will perform exactly one action.

A Once must not be copied after first use.

In the terminology of the Go memory model,
the return from f “synchronizes before”
the return from any call of once.Do(f).

Example [](#example-Once "Go to Example")

```None
package main

import (
	"fmt"
	"sync"
)

func main() {
	var once sync.Once
	onceBody := func() {
		fmt.Println("Only once")
	}
	done := make(chan bool)
	for i := 0; i < 10; i++ {
		go func() {
			once.Do(onceBody)
			done <- true
		}()
	}
	for i := 0; i < 10; i++ {
		<-done
	}
}
```

```None
Output:

Only once
```

Share
Format
Run

#### func (\*Once) [Do](https://cs.opensource.google/go/go/+/go1.20:src/sync/once.go;l=48) [](#Once.Do "Go to Once.Do")

```None
func (o *Once) Do(f func())
```

Do calls the function f if and only if Do is being called for the
first time for this instance of Once. In other words, given

```None
var once Once
```

if once.Do(f) is called multiple times, only the first call will invoke f,
even if f has a different value in each invocation. A new instance of
Once is required for each function to execute.

Do is intended for initialization that must be run exactly once. Since f
is niladic, it may be necessary to use a function literal to capture the
arguments to a function to be invoked by Do:

```None
config.once.Do(func() { config.init(filename) })
```

Because no call to Do returns until the one call to f returns, if f causes
Do to be called, it will deadlock.

If f panics, Do considers it to have returned; future calls of Do return
without calling f.

#### type [Pool](https://cs.opensource.google/go/go/+/go1.20:src/sync/pool.go;l=49) [¶](#Pool "Go to Pool") added in go1.3

```None
type Pool struct {

	// New optionally specifies a function to generate
	// a value when Get would otherwise return nil.
	// It may not be changed concurrently with calls to Get.
	New func() any
	// contains filtered or unexported fields
}
```

A Pool is a set of temporary objects that may be individually saved and
retrieved.

Any item stored in the Pool may be removed automatically at any time without
notification. If the Pool holds the only reference when this happens, the
item might be deallocated.

A Pool is safe for use by multiple goroutines simultaneously.

Pool's purpose is to cache allocated but unused items for later reuse,
relieving pressure on the garbage collector. That is, it makes it easy to
build efficient, thread-safe free lists. However, it is not suitable for all
free lists.

An appropriate use of a Pool is to manage a group of temporary items
silently shared among and potentially reused by concurrent independent
clients of a package. Pool provides a way to amortize allocation overhead
across many clients.

An example of good use of a Pool is in the fmt package, which maintains a
dynamically-sized store of temporary output buffers. The store scales under
load (when many goroutines are actively printing) and shrinks when
quiescent.

On the other hand, a free list maintained as part of a short-lived object is
not a suitable use for a Pool, since the overhead does not amortize well in
that scenario. It is more efficient to have such objects implement their own
free list.

A Pool must not be copied after first use.

In the terminology of the Go memory model, a call to Put(x) “synchronizes before”
a call to Get returning that same value x.
Similarly, a call to New returning x “synchronizes before”
a call to Get returning that same value x.

Example [](#example-Pool "Go to Example")

```None
package main

import (
	"bytes"
	"io"
	"os"
	"sync"
	"time"
)

var bufPool = sync.Pool{
	New: func() any {
		// The Pool's New function should generally only return pointer
		// types, since a pointer can be put into the return interface
		// value without an allocation:
		return new(bytes.Buffer)
	},
}

// timeNow is a fake version of time.Now for tests.
func timeNow() time.Time {
	return time.Unix(1136214245, 0)
}

func Log(w io.Writer, key, val string) {
	b := bufPool.Get().(*bytes.Buffer)
	b.Reset()
	// Replace this with time.Now() in a real logger.
	b.WriteString(timeNow().UTC().Format(time.RFC3339))
	b.WriteByte(' ')
	b.WriteString(key)
	b.WriteByte('=')
	b.WriteString(val)
	w.Write(b.Bytes())
	bufPool.Put(b)
}

func main() {
	Log(os.Stdout, "path", "/search?q=flowers")
}
```

```None
Output:

2006-01-02T15:04:05Z path=/search?q=flowers
```

Share
Format
Run

#### func (\*Pool) [Get](https://cs.opensource.google/go/go/+/go1.20:src/sync/pool.go;l=127) [](#Pool.Get "Go to Pool.Get") added in go1.3

```None
func (p *Pool) Get() any
```

Get selects an arbitrary item from the Pool, removes it from the
Pool, and returns it to the caller.
Get may choose to ignore the pool and treat it as empty.
Callers should not assume any relation between values passed to Put and
the values returned by Get.

If Get would otherwise return nil and p.New is non-nil, Get returns
the result of calling p.New.

#### func (\*Pool) [Put](https://cs.opensource.google/go/go/+/go1.20:src/sync/pool.go;l=95) [¶](#Pool.Put "Go to Pool.Put") added in go1.3

```None
func (p *Pool) Put(x any)
```

Put adds x to the pool.

#### type [RWMutex](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=36) [](#RWMutex "Go to RWMutex")

```None
type RWMutex struct {
	// contains filtered or unexported fields
}
```

A RWMutex is a reader/writer mutual exclusion lock.
The lock can be held by an arbitrary number of readers or a single writer.
The zero value for a RWMutex is an unlocked mutex.

A RWMutex must not be copied after first use.

If a goroutine holds a RWMutex for reading and another goroutine might
call Lock, no goroutine should expect to be able to acquire a read lock
until the initial read lock is released. In particular, this prohibits
recursive read locking. This is to ensure that the lock eventually becomes
available; a blocked Lock call excludes new readers from acquiring the
lock.

In the terminology of the Go memory model,
the n'th call to Unlock “synchronizes before” the m'th call to Lock
for any n < m, just as for Mutex.
For any call to RLock, there exists an n such that
the n'th call to Unlock “synchronizes before” that call to RLock,
and the corresponding call to RUnlock “synchronizes before”
the n+1'th call to Lock.

#### func (\*RWMutex) [Lock](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=141) [](#RWMutex.Lock "Go to RWMutex.Lock")

```None
func (rw *RWMutex) Lock()
```

Lock locks rw for writing.
If the lock is already locked for reading or writing,
Lock blocks until the lock is available.

#### func (\*RWMutex) [RLock](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=64) [](#RWMutex.RLock "Go to RWMutex.RLock")

```None
func (rw *RWMutex) RLock()
```

RLock locks rw for reading.

It should not be used for recursive read locking; a blocked Lock
call excludes new readers from acquiring the lock. See the
documentation on the RWMutex type.

#### func (\*RWMutex) [RLocker](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=224) [¶](#RWMutex.RLocker "Go to RWMutex.RLocker")

```None
func (rw *RWMutex) RLocker() Locker
```

RLocker returns a Locker interface that implements
the Lock and Unlock methods by calling rw.RLock and rw.RUnlock.

#### func (\*RWMutex) [RUnlock](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=111) [](#RWMutex.RUnlock "Go to RWMutex.RUnlock")

```None
func (rw *RWMutex) RUnlock()
```

RUnlock undoes a single RLock call;
it does not affect other simultaneous readers.
It is a run-time error if rw is not locked for reading
on entry to RUnlock.

#### func (\*RWMutex) [TryLock](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=166) [](#RWMutex.TryLock "Go to RWMutex.TryLock") added in go1.18

```None
func (rw *RWMutex) TryLock() bool
```

TryLock tries to lock rw for writing and reports whether it succeeded.

Note that while correct uses of TryLock do exist, they are rare,
and use of TryLock is often a sign of a deeper problem
in a particular use of mutexes.

#### func (\*RWMutex) [TryRLock](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=84) [¶](#RWMutex.TryRLock "Go to RWMutex.TryRLock") added in go1.18

```None
func (rw *RWMutex) TryRLock() bool
```

TryRLock tries to lock rw for reading and reports whether it succeeded.

Note that while correct uses of TryRLock do exist, they are rare,
and use of TryRLock is often a sign of a deeper problem
in a particular use of mutexes.

#### func (\*RWMutex) [Unlock](https://cs.opensource.google/go/go/+/go1.20:src/sync/rwmutex.go;l=198) [](#RWMutex.Unlock "Go to RWMutex.Unlock")

```None
func (rw *RWMutex) Unlock()
```

Unlock unlocks rw for writing. It is a run-time error if rw is
not locked for writing on entry to Unlock.

As with Mutexes, a locked RWMutex is not associated with a particular
goroutine. One goroutine may RLock (Lock) a RWMutex and then
arrange for another goroutine to RUnlock (Unlock) it.

#### type [WaitGroup](https://cs.opensource.google/go/go/+/go1.20:src/sync/waitgroup.go;l=23) [](#WaitGroup "Go to WaitGroup")

```None
type WaitGroup struct {
	// contains filtered or unexported fields
}
```

A WaitGroup waits for a collection of goroutines to finish.
The main goroutine calls Add to set the number of
goroutines to wait for. Then each of the goroutines
runs and calls Done when finished. At the same time,
Wait can be used to block until all goroutines have finished.

A WaitGroup must not be copied after first use.

In the terminology of the Go memory model, a call to Done
“synchronizes before” the return of any Wait call that it unblocks.

Example [](#example-WaitGroup "Go to Example")

This example fetches several URLs concurrently,
using a WaitGroup to block until all the fetches are complete.

```None
package main

import (
	"sync"
)

type httpPkg struct{}

func (httpPkg) Get(url string) {}

var http httpPkg

func main() {
	var wg sync.WaitGroup
	var urls = []string{
		"http://www.golang.org/",
		"http://www.google.com/",
		"http://www.example.com/",
	}
	for _, url := range urls {
		// Increment the WaitGroup counter.
		wg.Add(1)
		// Launch a goroutine to fetch the URL.
		go func(url string) {
			// Decrement the counter when the goroutine completes.
			defer wg.Done()
			// Fetch the URL.
			http.Get(url)
		}(url)
	}
	// Wait for all HTTP fetches to complete.
	wg.Wait()
}
```

Share
Format
Run

#### func (\*WaitGroup) [Add](https://cs.opensource.google/go/go/+/go1.20:src/sync/waitgroup.go;l=43) [](#WaitGroup.Add "Go to WaitGroup.Add")

```None
func (wg *WaitGroup) Add(delta int)
```

Add adds delta, which may be negative, to the WaitGroup counter.
If the counter becomes zero, all goroutines blocked on Wait are released.
If the counter goes negative, Add panics.

Note that calls with a positive delta that occur when the counter is zero
must happen before a Wait. Calls with a negative delta, or calls with a
positive delta that start when the counter is greater than zero, may happen
at any time.
Typically this means the calls to Add should execute before the statement
creating the goroutine or other event to be waited for.
If a WaitGroup is reused to wait for several independent sets of events,
new Add calls must happen after all previous Wait calls have returned.
See the WaitGroup example.

#### func (\*WaitGroup) [Done](https://cs.opensource.google/go/go/+/go1.20:src/sync/waitgroup.go;l=86) [¶](#WaitGroup.Done "Go to WaitGroup.Done")

```None
func (wg *WaitGroup) Done()
```

Done decrements the WaitGroup counter by one.

#### func (\*WaitGroup) [Wait](https://cs.opensource.google/go/go/+/go1.20:src/sync/waitgroup.go;l=91) [](#WaitGroup.Wait "Go to WaitGroup.Wait")

```None
func (wg *WaitGroup) Wait()
```

Wait blocks until the WaitGroup counter is zero.