Skip to content

Instantly share code, notes, and snippets.

View thrawn01's full-sized avatar
🎯
Focusing

Derrick J. Wippler thrawn01

🎯
Focusing
View GitHub Profile
@thrawn01
thrawn01 / smtp.go
Last active October 6, 2023 20:10
Basic SMTP Client
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/smtp"
)
@thrawn01
thrawn01 / main.go
Created September 18, 2023 16:14
HTTP gzip multipart form data client
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
@thrawn01
thrawn01 / retry_test.go
Created September 11, 2023 16:32
retry package which can be used with clients that implement `duh`
package duh
import (
"context"
"testing"
"time"
)
type DoThingRequest struct{}
type DoThingResponse struct{}
@thrawn01
thrawn01 / search.go
Created August 1, 2023 15:00
Some golang code to search all commit hashes in a repo, including lost and unreachable hashes to find the string 'steve'.
package main
import (
"bytes"
"fmt"
"os/exec"
"sort"
"strings"
"time"
)
@thrawn01
thrawn01 / tag-daily-notes.go
Created July 23, 2023 15:42
Tag all daily notes with #daily-notes
// This code is used to re-tag my daily notes from front matter style to `#tag` style and add
// a #daily-notes tag to each note. I'm saving it here as it might be useful as a template for
// others or myself in the future.
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
@thrawn01
thrawn01 / organize-daily-notes.go
Created July 23, 2023 15:39
Obsidian Script to Organize Daily Notes
/* This code organizes obsidian notes in format `YYYY-MM-DD.md` in the target directory into
the following folder structure `YYYY/MM Month/YYYY-MM-DD.md`
Examples
/2023/07 July/2023-07-26.md
/2023/08 August/2023-08-01.md
/2023/10 October/2023-10-31.md
*/
package main

Sorting

Everyone does it differently 😭 and most query string parsers don't support LHS or RHS parsing or building. Prefer RHS for clarity and simplicity of parsing. I don't like the - or + operators because It's easy to over look.

Options from different sources

  • ChatGPT /users?sort=email,asc
  • RHS Single Field /users?sort=email:ASC
  • RHS Multiple Fields /users?sort=email:asc,name:desc
  • RHS Multiple Fields Keyed /users?sort=email:asc&sort=name:desc
  • LHS /users?sort=[asc]email
  • LHS Multiple Fields /users?sort=[asc]email,[desc]name
@thrawn01
thrawn01 / description.md
Last active August 14, 2023 14:38
Fix This Code

This bit of code fetches 100 integers from an endpoint called https://example.com/integers which returns { "value": 2 } . The code makes 10 concurrent requests to the endpoint so we don't trigger a rate limit and then reports a list of which integers were even and which were odd.

How would you improve the following code? Feel free to rewrite as if this was production code. There are coding mistake and often just bad form in the code, please fix it!

@thrawn01
thrawn01 / dist.go
Created June 12, 2023 22:10
Distribution plot of rand.Shuffle() on a 3 item array 1000 times
package main
import (
"image/color"
"log"
"math/rand"
"gonum.org/v1/plot"
"gonum.org/v1/plot/plotter"
)
@thrawn01
thrawn01 / structToYAMLNode.go
Last active June 8, 2023 02:13
A bit of code to convert a struct into yaml.Node structure.
import "gopkg.in/yaml.v3"
func StructToYAMLNode(data interface{}, node *yaml.Node) error {
value := reflect.ValueOf(data)
switch value.Kind() {
case reflect.Struct:
// If the struct has a `String()` method call that instead
if strMethod := value.MethodByName("String"); strMethod.IsValid() {
obj := strMethod.Call(nil)[0].Interface()