Skip to content

Instantly share code, notes, and snippets.

@alexedwards
alexedwards / main.go
Created August 3, 2026 14:44
JSON encoding benchmarks
package main
import (
"encoding/json/jsontext"
"encoding/json/v2"
"net/http"
)
var data = map[string]string{
"status": "available",
@alexedwards
alexedwards / gist:dc3145c8e2e6d2fd6cd9
Last active July 31, 2026 16:17
Example of working with Go's database/sql and NULL fields
CREATE TABLE books (
isbn char(14) NOT NULL,
title varchar(255),
author varchar(255),
price decimal(5,2)
);
INSERT INTO books (isbn, title, author, price) VALUES
('978-1503261969', 'Emma', 'Jayne Austen', 9.44),
('978-1514274873', 'Journal of a Soldier', NULL, 5.49),
.
├── cmd
│   ├── cli
│   └── web
├── internal
│   ├── database
│   ├── request
│   ├── response
│   ├── templatefuncs
│   ├── validator
@alexedwards
alexedwards / main.go
Last active July 18, 2026 14:46
JSON encoding benchmarks
package main
import (
"encoding/json"
"net/http"
)
func main() {}
func healthcheckHandlerEncoder(w http.ResponseWriter, r *http.Request) {
@alexedwards
alexedwards / main.go
Last active June 25, 2026 23:39
Password hashing and verification with Argon2id
package main
import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"errors"
"fmt"
"log"
"strings"
.
├── books
│   ├── handlers.go
│   └── models.go
├── config
│   └── db.go
└── main.go
@alexedwards
alexedwards / Makefile
Last active April 21, 2026 10:31
Boilerplate Makefile for Go projects
# Change these variables as necessary.
main_package_path = ./cmd/example
binary_name = example
# ==================================================================================== #
# HELPERS
# ==================================================================================== #
## help: print this help message
.PHONY: help
@alexedwards
alexedwards / logger.go
Last active April 14, 2026 08:08
Levelled and structure logger using log package only
package logger
import (
"fmt"
"log"
"os"
"strings"
)
type Level int
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Simple Password - Returns a simple pronouncable passphrase.
Examples: Motir27, Wesag97, Muvak41
Rated as 'good' by http://www.passwordmeter.com/
@access public
@return string
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function simple_password() {
protected $authentication = 'none'; // Can be 'none', 'basic' or 'digest'
protected $username = FALSE;
protected $password = FALSE;
protected $key = FALSE;
protected $format = 'html';
protected $supported_formats = array(
'html' => 'text/html',
'php' => 'text/plain',