Skip to content

Instantly share code, notes, and snippets.

@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
// longestCommonPrefix returns the string in compares that has the longest common prefix with s
func longestCommonPrefix(s string, compares ...string) string {
var longestMatched int
var longestCmp string
maxLength := len(s)
for _, c := range compares {
cmpLen := len(c)
if cmpLen > maxLength {
cmpLen = maxLength
}
function ask_yes_no() {
local prompt="$1"
while true; do
read -p "$prompt [y/n]?" yn
case "$yn" in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer y or n";;
esac
done
package main
import (
"path"
"syscall"
)
// isMountPoint returns true if the given directory is a mountpoint
func isMountPoint(fileName string) (bool, error) {
dir := path.Clean(fileName)
@gammazero
gammazero / wp_track_jobs.go
Last active October 29, 2020 21:11
Track workerpool job completion
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/gammazero/workerpool"
)