Skip to content

Instantly share code, notes, and snippets.

@creachadair
creachadair / type-parameter-iface.md
Created July 22, 2023 22:05
Checking type parameters for optional interface satisfaction in Go

Checking Type Parameters for Optional Interface Satisfaction

With Go generics, it is not permitted to check whether a value whose type comes from a type parameter satisfies some optional interface. For example, the following code will not compile (as of Go 1.20):

type Q interface{ Q() }

func G[T any](t T) {
	if q, ok := t.(Q); ok {
 q.Q()
@creachadair
creachadair / mjpeg.md
Created April 12, 2023 05:54
MJPEG notes
@creachadair
creachadair / underlying.md
Last active March 26, 2023 17:04
Underlying types in Go generics

Use of Underlying Types in Go Generics

When specifying the type parameters for a generic structure in Go, you can use the notation ~T to mean "any type whose underlying structure is equivalent to T". For example, given:

func F[X ~string](x X) {}

the type parameter X can be instantiated with any type whose underlying representation is string. That includes string itself, of course, along with types like type Foo string which are based on it.

@creachadair
creachadair / chords.json
Last active January 10, 2023 00:12
Guitar chord database
{"chords":[{"name":"?","play":"X 0 4 4 3 0"},{"name":"?","play":"X 0 4 0 0 0"},{"name":"A","play":"X X 0 9 10 9"},{"name":"A","play":"0 0 2 2 2 0"},{"name":"A","play":"5 7 7 6 5 5"},{"name":"A#7","alt":["Bb7"],"play":"X X 3 3 3 4"},{"name":"A#7","alt":["Bb7"],"play":"X X 3 3 3 4"},{"name":"A(add B)","play":"0 0 2 4 2 0"},{"name":"A(add B)","play":"X 0 7 6 0 0"},{"name":"A(no 3rd)","play":"X 0 2 2 X 0"},{"name":"A/C#","play":"0 4 X 2 5 0"},{"name":"A/C#","play":"X 4 2 2 2 X"},{"name":"A/C#","play":"X 4 7 X X X"},{"name":"A/D","play":"X X 0 6 5 5"},{"name":"A/E","play":"0 0 2 2 2 0"},{"name":"A/G","play":"3 X 2 2 2 0"},{"name":"A13","play":"0 0 2 0 2 2"},{"name":"A5","play":"X 0 2 2 X X"},{"name":"A5","play":"5 7 7 X X X"},{"name":"A6","play":"X X 0 14 14 14"},{"name":"A6","play":"0 0 2 2 2 2"},{"name":"A6sus4","play":"X 0 2 2 3 2"},{"name":"A7","play":"X 0 2 2 2 3"},{"name":"A7sus4","play":"X 0 2 2 3 3"},{"name":"A9","play":"X X 0 12 12 12"},{"name":"Ab","play":"X X 6 5 4 4"},{"name":"Ab","play":"4 6 6 5 4 4"}
@creachadair
creachadair / main_test.go
Last active May 19, 2022 16:01
Microbenchmark throughput latency of buffered channels
package main_test
import (
"fmt"
"testing"
)
func BenchmarkBuf(b *testing.B) {
for _, bs := range []int{0, 1, 2, 10, 32} {
b.Run(fmt.Sprint(bs), func(b *testing.B) {
@creachadair
creachadair / wake-on-change.md
Last active August 12, 2026 19:53
Waiting for a Value to Change in Go

Waiting for a Value to Change in Go

When multiple goroutines share access to a value that will periodically change, readers may wish to wait for a value to be updated before reading the value again. This can be solved using a condition variable:

var val *Thing
var mu = new(sync.Mutex)
var cond = sync.NewCond(mu)

func wait(old *Thing) *Thing {
@creachadair
creachadair / gh-run.sh
Last active May 10, 2022 15:40
A script to retry a failed GitHub actions run.
#!/bin/zsh
set -euo pipefail
readonly pollsec=5
if [[ "$#" = 0 ]] ; then
echo "Missing required run ID" 1>&2
exit 2
fi
@creachadair
creachadair / insert-text.md
Last active December 11, 2022 23:42
Inserting text at cursor on macOS

Insert Text at Cursor on macOS

Requirement: Enable Accessibility for the Terminal application.

tell application "System Events"
   keystroke "text to be inserted\n"
end tell
@creachadair
creachadair / award-checklist.md
Last active October 21, 2021 23:32
Hugo, Nebula and Clarke Winners Checklist

Hugo, Nebula and Clarke Winners Checklist

Extracted from the Google form posted in https://twitter.com/hammard_1987/status/1449453485343318025.

  • Children of Time (Adrian Tchaikovsky)
  • Rite of Passage (Alexei Panshin)
  • The Demolished Man (Alfred Bester)
  • The Calcutta Chromosome (Amitav Ghosh)
  • Ancillary Justice (Ann Leckie)
  • Dreams Before the Start of Time (Anne Charnock)