Skip to content

Instantly share code, notes, and snippets.

View mdwhatcott's full-sized avatar

Michael Whatcott mdwhatcott

View GitHub Profile
@mdwhatcott
mdwhatcott / slog_shortfile.go
Created December 6, 2022 18:18
Achieving log.Lshortfile with the new slog package
// https://pkg.go.dev/golang.org/x/exp/slog
func main() {
options := slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(a slog.Attr) slog.Attr {
if a.Key != "source" {
return a
}
fields := strings.Split(a.Value.String(), ":")
type Slice struct {
Data []int
Length int
Capacity int
}
func Insert(record *Slice, position int, value int) {
capacity := record.Length + 1
if position > capacity {
capacity = position
@mdwhatcott
mdwhatcott / http_get.go
Created January 17, 2023 22:22
A simple http get example, using the Smarty US Street Address API (but no error handling or deferred functions)
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
)
@mdwhatcott
mdwhatcott / http_get.go
Created January 17, 2023 21:59
A simple http get example, using the Smarty US Street Address API (but everything is in the main function)
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
)
@mdwhatcott
mdwhatcott / http_get.go
Created January 16, 2023 23:54
A simple http get example, using the Smarty US Street Address API
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
)
@mdwhatcott
mdwhatcott / mini-smartylist.go
Created January 16, 2023 23:53
A tiny address list processor
package main
import (
"encoding/csv"
"encoding/json"
"io"
"log"
"net/http"
"os"
)
@mdwhatcott
mdwhatcott / stringly.go
Last active January 13, 2023 00:00
Go is stringly typed, right?
package main
import (
"fmt"
"reflect"
)
type (
MyFirstEvent struct{ AccountID uint64 }
MySecondEvent struct{ AccountID uint64 }
@mdwhatcott
mdwhatcott / custom_json.go
Created July 29, 2015 17:15
Example of implementing MarshalJSON and UnmarshalJSON to serialize and deserialize custom types to JSON in Go. Playground: http://play.golang.org/p/7nk5ZEbVLw
package main
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
)
func main() {
@mdwhatcott
mdwhatcott / funcy.go
Created December 29, 2022 10:18
Map/Reduce/Filter (and a few friends) w/ Go generics
// Package funcy info: github.com/mdwhatcott/funcy@v0.1.0 (a little copy-paste is better than a little dependency)
// AUTO-GENERATED: 2022-12-29 03:18:09.371452 -0700 MST m=+0.000761384
package funcy
// FILE: funcy.go
func Complement[T any](predicate func(t T) bool) func(t T) bool {
return func(t T) bool { return !predicate(t) }
}
func Remove[T any](predicate func(t T) bool, values []T) []T {
@mdwhatcott
mdwhatcott / should.go
Last active December 28, 2022 06:57
Single-file version of: github.com/mdwhatcott/testing/should
// Package should info: github.com/mdwhatcott/testing@v0.19.0 (a little copy-paste is better than a little dependency)
// AUTO-GENERATED: 2022-12-27 23:56:46.913568 -0700 MST m=+0.004900728
package should
import (
"errors"
"fmt"
"log"
"math"
"reflect"