Skip to content

Instantly share code, notes, and snippets.

@scottcagno
scottcagno / final-repo.go
Last active January 20, 2024 22:05
Generic Repository idea and implementation
package api
import (
"errors"
"sync"
)
// QueryFunc is a function that is injected with a type
// and returns a boolean
type QueryFunc[T any] func(t T) bool
@scottcagno
scottcagno / trace.go
Last active October 20, 2023 21:47
Golang tracer (helpful for debugging sometimes)
// MUST BE WRAPPED, or depth MUST BE CHANGED to 2
func Trace() string {
depth := 3
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(depth, pc)
f := runtime.FuncForPC(pc[0])
file, line := f.FileLine(pc[0])
sfile := strings.Split(file, "/")
sname := strings.Split(f.Name(), "/")
return fmt.Sprintf("[%s:%d %s]", sfile[len(sfile)-1], line, sname[len(sname)-1])
@scottcagno
scottcagno / app.go
Last active September 25, 2023 21:15
Web framework idea...
package app
import (
"fmt"
"log/slog"
"net/http"
"os"
"sort"
"strings"
"sync"
@scottcagno
scottcagno / search.go
Created September 19, 2023 20:54
Simple beginnings of a search engine type thing...
package main
import (
"fmt"
"io"
"log"
"os"
"regexp"
"sort"
"strconv"
@scottcagno
scottcagno / bin.go
Last active July 26, 2023 17:29
simple binary encoding and decoding
package main
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"math"
@scottcagno
scottcagno / table_dto.go
Created January 30, 2023 18:56
General purpose DTO in Go
package main
import (
"fmt"
"reflect"
)
type User struct {
ID int
Name string
@scottcagno
scottcagno / main.go
Created January 23, 2023 07:59
Poller.go
package main
import (
"fmt"
"log"
"sync"
"time"
)
func main() {
@scottcagno
scottcagno / api-new-idea.go
Last active December 21, 2022 21:27
API ideas and examples
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"reflect"
"runtime"
"strconv"
@scottcagno
scottcagno / poller_ticker.go
Created November 24, 2022 21:10
Poller Ticker final
https://go.dev/play/p/oAUq-zpkhhp
@scottcagno
scottcagno / main.go
Created October 26, 2022 22:28
RESTful API Idea in Go
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
)