Skip to content

Instantly share code, notes, and snippets.

@gammazero
gammazero / gob_pointers.go
Created March 21, 2025 00:11
gob encode and decode pointers
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
}
@gammazero
gammazero / async_iter.go
Last active October 4, 2024 15:34
Asynchronous Go iterator
// 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.
//
@gammazero
gammazero / slice_elements_match.go
Created July 12, 2024 19:12
Compare slices ignoring order of elements
// 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
}

Channel Behaviors

Reading a channel

  • 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

Writing a channel

  • A send to a nil channel blocks forever
  • A send to a closed channel panics
package main
import (
"flag"
"strings"
)
type arrayFlags []string
func (a *arrayFlags) String() string {
@gammazero
gammazero / merge.go
Created February 2, 2024 04:58
Merge K sorted lists
package main
import (
"container/heap"
)
type priorityQueue [][]int
func (pq priorityQueue) Len() int { return len(pq) }
@gammazero
gammazero / option.go
Last active December 20, 2022 20:52
Option template
package server
import (
"fmt"
"time"
)
const (
defaultWriteTimeout = 30 * time.Second
defaultReadTimeout = 30 * time.Second
@gammazero
gammazero / file_exists.go
Last active November 10, 2022 02:10
Check if files and directories exist
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
@gammazero
gammazero / hash_bench_test.go
Created November 23, 2021 20:54
Benchmark different hashes
package main
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"hash"
"hash/fnv"
"testing"
@gammazero
gammazero / channel_notes.txt
Created November 2, 2021 16:09
Golang channel notes
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