- A receive from a nil channel blocks forever
- A receive from a closed channel returns the zero value immediately
- A receive on an empty channel blocks
- A send to a nil channel blocks forever
- A send to a closed channel panics
func encodePointer(encoder *gob.Encoder, ptr any) error { | |
isNil := ptr == nil || (reflect.ValueOf(ptr).Kind() == reflect.Ptr && reflect.ValueOf(ptr).IsNil()) | |
if err := encoder.Encode(isNil); err != nil { | |
return err | |
} |
// This example demonstrates getting a series of results and an error from a | |
// goroutine, with the ability to cancel the goroutine. The goroutine can be | |
// canceled by stopping iteration (break out of the iteration loop), or by | |
// canceling a context, as may be the case if a context from outside of the | |
// iteration logic is provided. | |
// | |
// The first part of the example shows using channels to read the results and | |
// error, and using a context to cancel the goroutine before all results are | |
// received. | |
// |
// ElementsMatch compares two slices ignoring the order of the elements. If | |
// there are duplicate elements, the number of appearances of each of them in | |
// both lists should match. Empty and nil slices are treated as equal. | |
func ElementsMatch[S ~[]E, E comparable](s1, s2 S) bool { | |
f len(s1) != len(s2) { | |
return false | |
} | |
if len(s1) == 0 && len(s2) == 0 { | |
return true | |
} |
package main | |
import ( | |
"flag" | |
"strings" | |
) | |
type arrayFlags []string | |
func (a *arrayFlags) String() string { |
package main | |
import ( | |
"container/heap" | |
) | |
type priorityQueue [][]int | |
func (pq priorityQueue) Len() int { return len(pq) } |
package server | |
import ( | |
"fmt" | |
"time" | |
) | |
const ( | |
defaultWriteTimeout = 30 * time.Second | |
defaultReadTimeout = 30 * time.Second |
func fileExists(filename string) bool { | |
_, err := os.Stat(filename) | |
return !errors.Is(err, fs.ErrNotExist) | |
} | |
func dirExists(name string) (bool, error) { | |
fi, err := os.Stat(name) | |
if err != nil { | |
if errors.Is(err, fs.ErrNotExist) { | |
return false, nil |
package main | |
import ( | |
"crypto/md5" | |
"crypto/sha1" | |
"crypto/sha256" | |
"crypto/sha512" | |
"hash" | |
"hash/fnv" | |
"testing" |
Channel Behaviors | |
* A receive from a nil channel blocks forever | |
* A receive from a closed channel returns the zero value immediately | |
* A receive on an empty channel blocks | |
* A send to a nil channel blocks forever | |
* A send to a closed channel panics | |
* A send to a full (or unbuffered) channel blocks until reader has read data |