Skip to content

Instantly share code, notes, and snippets.

const WRITE_TIME = 10 * time.Second
const PING_TIME = 60 * time.Second
const PONG_TIME = PING_TIME + WRITE_TIME
func pingPong(conn *websocket.Conn) {
ticker := time.NewTicker(PING_TIME)
defer func() {
ticker.Stop()
conn.Close()
#!/bin/bash
# Generate a new private key of size 4096 (with no pass phrase)
openssl genrsa -out server-key.pem 4096
# Generate a self-signed cert using the key (valid for 10 years)
openssl req -new -x509 -key server-key.pem -out server-cert.pem -days 3650
# Inspect the new cert
openssl x509 -in server-cert.pem -noout -text
// Source: https://github.com/mattn/go-sqlite3/pull/116
if err.(sqlite3.Error).Code == sqlite3.ErrConstraint {
// clumsily compare err.Error() against strings to figure out what the heck happened...
}
// Additionally, the following is now possible:
if err.(sqlite3.Error).ExtendedCode == sqlite3.ErrConstraintUnique {
// oh! my unique constraint failed?
package main
import (
"fmt"
"io"
"os"
"golang.org/x/crypto/ssh/terminal"
)
x, y = float
radians = atan2(y,x)
degrees = radians * (180 / pi)
Source: https://en.wikipedia.org/wiki/Circle#Equations
In an x–y Cartesian coordinate system, the circle with centre coordinates (a, b)
and radius r is the set of all points (x, y) such that:
x = a + r * cos t
y = b + r * sin t
where t is in the range 0 to 2π, interpreted geometrically as the angle that the
ray from (a, b) to (x, y) makes with the positive x-axis.
@lmas
lmas / generate_id.go
Created September 26, 2016 19:11
Generate short-ish alphanumeric IDs
package genid
import (
"crypto/sha512"
"encoding/hex"
)
// NOTICE: DO NOT RELY ON THIS BEING SECURE!
// 16 hex chars = 64 entropy bits (in other words, worse than MD5 = 128 bits)
// but theoretically we should be good when using less than 10K ids?
@lmas
lmas / icmp_ping.go
Last active April 18, 2024 11:45
Simple utility package to send ICMP pings with Go
// Copyright © 2016 Alex
//
// This program is free software: you can redistribute it and/or modify it under
// the terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
@lmas
lmas / confirm.txt
Created October 12, 2016 10:19
Simple html/js confirmation box
<a href="/delete/ITEM" onClick="return confirm('You sure you want to delete this item?');">Delete</a>
requires only vanilla js
shows a ok/cancel popup box with a msg
clicking ok makes the user continue to the link
@lmas
lmas / generate_id.go
Last active November 14, 2019 17:16
Generate 16 bytes long IDs (9 bytes based on time and 7 bytes random data), that's base36 encoded for nice URL safety and unique IDs
package main
// Generate 16 bytes long IDs.
// 9 bytes time and 7 bytes random, and then base36 encoded for nice URL safety.
//
// With heavy inspiration from:
// https://github.com/oklog/ulid
import (
"crypto/rand"