Skip to content

Instantly share code, notes, and snippets.

Avatar
🦄
Vague, but exciting...

Beth Anderson betandr

🦄
Vague, but exciting...
View GitHub Profile
@betandr
betandr / main.go
Last active Aug 6, 2020
Sum a tree using nil pointer receivers
View main.go
package main
import "fmt"
type node struct {
v int
l *node
r *node
}
@betandr
betandr / done_channel.go
Created Jul 8, 2020
Go "done-channel" pattern
View done_channel.go
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 Jul 8, 2020
An interface is only considered nil if it is not associated with an underlying value, even a nil value.
View errors.go
package main
import "fmt"
type Error struct {
Message string
}
func (e *Error) Error() string {
return "an error occured"
@betandr
betandr / main.go
Last active Apr 27, 2020
Fitbit Export Weight Data to CSV
View main.go
// 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 Jan 16, 2020
Generate keys with crypto/rand
View main.go
// 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 Dec 30, 2019
Make multiple queries and return the fastest in Go, using goroutines and a buffered channel
View query.go
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
// multiQuery uses a buffered channel, otherwise the two slower goroutines
@betandr
betandr / pipeline.go
Created Dec 30, 2019
Simple pipeline example using Go channels
View pipeline.go
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
}
View gist:09e044a979a66a76893df950d823bd2e
ffmpeg -i test.mov \
-pix_fmt yuv420p \
-vcodec libx264 \
-vf scale=640:-1 \
-acodec aac \
-vb 1024k \
-minrate 1024k \
-maxrate 1024k \
-bufsize 1024k \
-ar 44100 \
@betandr
betandr / interface.go
Created Jul 31, 2019
Encapsulation with an interface
View interface.go
package account
import (
"errors"
)
type CheckingAccount struct {
balance int
}
@betandr
betandr / primes.go
Created Jul 25, 2019
Primes generator using filters and pipes pattern
View primes.go
package main
import (
"fmt"
)
func main() {
// runtime.GOMAXPROCS(2)
ch := make(chan int)
You can’t perform that action at this time.