Skip to content

Instantly share code, notes, and snippets.

@creachadair
creachadair / remove-taint.sh
Last active August 21, 2026 17:50
Remove quarantine taint from files on macOS
#!/usr/bin/env bash
#
# Usage: remove-taint.sh <filename>
#
# Remove the annoying mark-of-the-web taint xattrs from a file.
# For some reason macOS ignores a removexattr for these attributes,
# but the taint does not survive a pipe copy and rename.
#
# Note that the com.apple.macl xattr (Apple's analogue of the mark-of-the-web)
# is special, and the system will track it across activity on the data by the
@creachadair
creachadair / self-signed-tls.md
Last active November 24, 2024 23:54
Self-signed TLS certificates

Creating Self-Signed TLS Certificates

(see also: https://github.com/creachadair/tlsutil)

A "CA" certificate is basically just a self-signed certificate that someone has blessed. There are a few pedantic details you have to get right in the cert settings if you want a browser to accept it (particularly an older browser), but CLI tools seem to be less picky.

@creachadair
creachadair / go-mod-proxy.md
Last active August 23, 2024 04:56
Notes on building a Go module caching proxy
@creachadair
creachadair / go-experiment.md
Last active November 24, 2024 23:47
Installing a Go toolchain with a GOEXPERIMENT set

Installing a Go Toolchain with GOEXPERIMENT

(see also: https://github.com/creachadair/misctools/blob/main/scripts/install-go.sh)

Go allows the definition of named, [experimental features][xpkg], which are established by setting GOEXPERIMENT in the environment. While some of these features are [available at runtime][xrun], some must be enabled when building the toolchain itself. An example of the latter is the [cacheprog experiment][xcp] (as of August 2024).

To enable such an experiment:

  1. Download a source distribution of Go.
  2. Unpack the distribution and cd go/src.
@creachadair
creachadair / generic-string.md
Last active March 11, 2024 15:36
A cute trick with strings

Strings and Bytes: Together Again

Sometimes you want your Go function to accept either a string or a slice of bytes, at which point the user then has to convert whichever one they have to the version you chose:

func Process(s string) { /* … do stuff with s … */ }

// …
var someBytes = []byte(/* … */)
pkg.Process(string(someBytes))
@creachadair
creachadair / vm-shutdown.sh
Last active November 18, 2024 22:24
Set or refresh a timed shutdown of a machine
#!/usr/bin/env bash
#
# Usage: vm-shutdown.sh [--check|--cancel|--help]
#
# Schedule a shutdown of the system at a time in the future.
# With --check, do not schedule a shutdown, just print the scheduled time.
# With --cancel, cancel a pending shutdown.
#
# Installation:
#
@creachadair
creachadair / packed-length.md
Last active March 10, 2026 05:25
A self-framing packed binary format for unsigned integer lengths

Self-Framing Packed Binary Lengths

We describe a variable-width binary encoding format for unsigned integer lengths that handles values up to 1073741823 $(2^{30} - 1)$ and does not require complex bit manipulation to pack and unpack.

In summary: The input is encoded as little-endian, with the excess length of the encoding packed into the lowest-order 2 bits of the value. This makes the encoding self-framing, as the decoder can consume the first byte and immediately know how many additional bytes must be read to recover the original value. The only operations required are integer addition, multiplication, and division/remainder by powers of 2 (specificially, 4 and 256).

The algorithm below is described for 32-bit unsigned integers, but it can be extended trivially to 64-bit integers by using the lowest-order 3 bits instead of 2, or contracted to 16-bit integers using the lowest-order 1 bit.

A Go implementation

@creachadair
creachadair / gh-heading-anchors.md
Last active October 10, 2023 16:35
GitHub Heading Anchors and You: A Tale of Needless and Undocumented Complexity

GitHub renders Markdown in its UI according to the [GitHub Flavored Markdown (GFM)][gfm] spec. In addition, it adds generated link anchors for document headers. For example, a Markdown heading like:

## Fee Fie Fo Fum

is rendered with a link anchor of #fee-fie-fo-fum in the GitHub UI. Knowing this, you may wish to add links in other Markdown documents in your repository that point to those anchor targets. There are various descriptions floating around about how to do this, but all the ones I found contain subtle errors. This document attempts to clarify the rule.

Definitions

@creachadair
creachadair / options.md
Last active July 28, 2023 23:12
My current preferred option plumbing style in Go

Option Plumbing in Go

This gist outlines my current preferred library package organization style in Go. There are a few explanatory comments, but for the most part it's just an extended example.

// Package thing is named for the concept it implements.
package thing

// Thing is the principal type that implements the concept.
// Although this does stutter a little with the package name,
@creachadair
creachadair / pointer-construction.md
Last active July 26, 2023 14:00
Constructing pointers to type parameters in Go

Constructing Pointers to Type Parameters

I wanted to have a function that could construct a value satisfying an interface, do some setup on that value, and then pass the constructed value to some other operation. Schematically:

func doAThing(fn func(*Input) (Output, error)) (Output, error) {
    in := new(Input)
    in.Setup()
    return fn(in)
}