- 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
| // 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 | |
| } |
| // 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. | |
| // |
| 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 | |
| } |
| package main | |
| import "fmt" | |
| // BitStrings generates all combinations of binary values, as strings, having | |
| // the specified number of bits. | |
| func BitStrings(n int) []string { | |
| if n <= 0 { | |
| return nil | |
| } |