Skip to content

Instantly share code, notes, and snippets.

View koonix's full-sized avatar
🦆
honk

koonix

🦆
honk
View GitHub Profile
@koonix
koonix / surfboard.sh
Created September 10, 2022 22:00
like surfraw, but not a million fucking lines long.
#!/bin/bash
# like surfraw, but not a million fucking lines long.
# requires dmenu.
# the default search engine is shown at first.
# to select another search engine, press enter when
# the prompt is empty in dmenu.
# name of the default search engine
default='Searx'
@koonix
koonix / random_bytes.go
Last active January 7, 2025 23:41
Go random byte generator
import "math/rand/v2"
func randomBytes(dst []byte) {
const charset = "abcdefghijklmnopqrstuvwxyz" +
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\t\n "
for i := 0; i < len(dst); i++ {
dst[i] = charset[rand.IntN(len(charset))]
}
}
@koonix
koonix / seen.go
Created September 2, 2024 16:12
Go unique value tester.
package seen
type Seen[T comparable] struct {
Map map[T]struct{}
}
func New[T comparable](size int) Seen[T] {
return Seen[T]{
Map: make(map[T]struct{}, size),
}
@koonix
koonix / addlicense.sh
Created September 5, 2024 12:33
Add license headers to source files. This example adds the Apache header to Golang files.
#!/bin/bash
set -eu -o pipefail
# This script ensures source code files
# have copyright license headers.
#
# It modifies all source files in place
# and avoids adding a license header
# to any file that already has one.
@koonix
koonix / withcloser_reader.go
Last active September 18, 2024 17:30
Go package that adds Close to Reader/Writer
package withcloser
import "io"
type reader struct {
io.Reader
close func() error
}
func (r reader) Close() error {
@koonix
koonix / once.go
Created September 27, 2024 23:04
Go object that stores a value only once
package once
import "sync"
type Once[T any] struct {
sync.Mutex
has bool
v T
}
@koonix
koonix / nuclear.go
Last active January 8, 2025 00:14
Nuclear atomically stores values. The closest thing to this in the stdlib is atomic.Pointer.
import "sync"
type Nuclear[T any] struct {
mu sync.RWMutex
v T
}
func NewNuclear[T any](v T) *Nuclear[T] {
return &Nuclear{
v: v,
@koonix
koonix / permute.go
Last active October 6, 2024 17:32
Go function that returns the permutations of a slice.
func permute[T any](v []T) [][]T {
switch len(v) {
case 0:
return [][]T{
{},
}
case 1:
return [][]T{
{v[0]},
}
@koonix
koonix / subsets.go
Last active October 6, 2024 17:35
Go function that returns the subsets of a slice.
func subsets[T any](v []T) [][]T {
switch len(v) {
case 0:
return [][]T{
{},
}
case 1:
return [][]T{
{v[0]},
{},
@koonix
koonix / doh-servers.sh
Created December 21, 2024 21:02
Script that returns a list of DNS-over-HTTPS servers that are accessible directly via IP address.
#!/bin/bash
set -eu -o pipefail
# Script that returns a list of DNS-over-HTTPS servers that are accessible directly via IP address.
# https://dnscrypt.info/public-servers
# https://dnscrypt.info/stamps-specifications
main()
{
VERBOSE=false