Skip to content

Instantly share code, notes, and snippets.

View betandr's full-sized avatar
🦄
Vague, but exciting...

Beth Anderson betandr

🦄
Vague, but exciting...
View GitHub Profile
@betandr
betandr / semver.go
Created September 27, 2022 10:22
Semver
package main
import (
"flag"
"fmt"
"github.com/Masterminds/semver/v3"
)
var vFlag = flag.String("version", "0.1", "version to check")
@betandr
betandr / indices.go
Created February 11, 2022 15:48
Return indices of two numbers summed matching a target
// Given an array of integers that is already sorted in ascending
// order find two numbers such that they add up to a specific target number.
//
// The function twoSum should return indices of the two numbers such that
// they add up to the target where index1 must be less than index2.
package main
import (
"errors"
"fmt"
@betandr
betandr / generics.go2
Last active June 3, 2021 15:09
Go 2 Generics
// Go 2 Playground: https://go2goplay.golang.org/
//
// Or to install the go2go tool:
// git clone https://go.googlesource.com/go goroot
// cd ./goroot
// git checkout dev.go2go
// cd ./src
// CGO_ENABLED=0 ./all.bash
// export PATH=/path/to/your/goroot/bin:$PATH
//
@betandr
betandr / main.go
Last active August 6, 2020 13:56
Sum a tree using nil pointer receivers
package main
import "fmt"
type node struct {
v int
l *node
r *node
}
@betandr
betandr / done_channel.go
Created July 8, 2020 15:43
Go "done-channel" pattern
package main
import (
"fmt"
"time"
)
// multiples uses a done channel to shut down
func multiples(i int) (chan int, chan struct{}) {
out := make(chan int)
@betandr
betandr / errors.go
Last active July 8, 2020 13:56
An interface is only considered nil if it is not associated with an underlying value, even a nil value.
package main
import "fmt"
type Error struct {
Message string
}
func (e *Error) Error() string {
return "an error occured"
@betandr
betandr / main.go
Last active March 4, 2024 01:34
Fitbit Export Weight Data to CSV
// Converts the data created by Fitbit export from the output JSON
// to comma-separated values.
// go run main.go > weight.csv
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
@betandr
betandr / main.go
Created January 16, 2020 15:15
Generate keys with crypto/rand
// Do not use package math/rand to generate keys, even throwaway ones.
// Unseeded, the generator is completely predictable. Seeded with
// time.Nanoseconds(), there are just a few bits of entropy. Instead,
// use crypto/rand's Reader, and if you need text, print to hexadecimal
// or base64.
package main
import (
"crypto/rand"
"fmt"
@betandr
betandr / query.go
Last active December 30, 2019 14:47
Make multiple queries and return the fastest in Go, using goroutines and a buffered channel
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
// multiQuery uses a buffered channel, otherwise the two slower goroutines
@betandr
betandr / pipeline.go
Created December 30, 2019 13:51
Simple pipeline example using Go channels
package main
import "fmt"
// first adds 20 integers in sequence to a channel then closes
// it, signalling all of the writing is done.
func first(out chan<- int) {
for x := 0; x < 20; x++ {
out <- x
}