Skip to content

Instantly share code, notes, and snippets.

@lmas
lmas / sort_struct.go
Last active January 13, 2016 17:17
Sort a Go struct based on a string value.
package main
import (
"fmt"
"sort"
)
// Our custom struct to sort.
type Data struct {
Name string
@lmas
lmas / stdin.go
Created January 18, 2016 21:43
stding.go - Read a Go string from STDIN
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
@lmas
lmas / round.go
Created February 23, 2016 20:34
round.go - Round a float64 in Go
package main
func RoundPrec(x float64, prec int) float64 {
if math.IsNaN(x) || math.IsInf(x, 0) {
return x
}
sign := 1.0
if x < 0 {
@lmas
lmas / cli_session.go
Created March 5, 2016 22:42
Start multiple terminal/bash session in the same path.
package main
import (
"os"
"os/exec"
)
// Start a new terminal/bash session in specified path.
func session(path string) {
os.Chdir(path)
#!/bin/bash
EMAIL="your@email.com"
LEGOROOT="/path/to/lego"
function renew {
domain="$1"
path="$2"
$LEGOROOT/lego --key-type rsa4096 --accept-tos --path "$LEGOROOT/.lego" --email "$EMAIL" \
package leven
import "github.com/arbovm/levenshtein"
func calcScore(a, b string) float64 {
score := levenshtein.Distance(a, b)
lena := len(a)
lenb := len(b)
var bigger float64
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"
)