Skip to content

Instantly share code, notes, and snippets.

View VojtechVitek's full-sized avatar
🏄
https://golang.cz

Vojtech Vitek (golang.cz) VojtechVitek

🏄
https://golang.cz
View GitHub Profile
@VojtechVitek
VojtechVitek / fibonacci_closure.go
Created April 14, 2014 10:44
A Tour of Go | Exercise: Fibonacci closure
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
fib := 0
return func() int {
first, second := 0, 1
@VojtechVitek
VojtechVitek / rot13_reader.go
Created April 15, 2014 16:50
A Tour of Go - Exercise: Rot13 Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
@VojtechVitek
VojtechVitek / run_jobs_parallel.sh
Created July 9, 2014 15:44
Bash script - Run commands/jobs in parallel and attach to its output and wait for the return codes sequentially
# Run job in background, redirect its output to a non-blocking pipe,
# and leave the rc and stdout/stderr to be handled by attach_wait_jobs()
jobs_tmp_dirs=""
function run_job() {
local dir=$(mktemp -d -t gearXXXXXX)
jobs_tmp_dirs="$jobs_tmp_dirs $dir"
mkfifo $dir/pipe
mkfifo $dir/pipe_nonblock
"$@" &>$dir/pipe_nonblock &
echo $! >$dir/pid
@VojtechVitek
VojtechVitek / map-type-reflect.go
Created September 24, 2014 11:27
Golang - How to get an underlying type of a map using reflect pkg
package main
import (
"fmt"
"reflect"
)
type Map map[string]string
type Object struct {
@VojtechVitek
VojtechVitek / redirects.go
Last active July 7, 2023 19:10
Golang: Workaround for too many redirects - "stopped after 10 redirects" error
sourceURL := "http://example.com"
// Resolve URL up to 12 redirects.
client := &http.Client{
CheckRedirect: func() func(req *http.Request, via []*http.Request) error {
redirects := 0
return func(req *http.Request, via []*http.Request) error {
if redirects > 12 {
return errors.New("stopped after 12 redirects")
}
@VojtechVitek
VojtechVitek / subrouters.go
Last active January 26, 2016 02:43
subrouters.go
package main
import (
"log"
"net/http"
"github.com/pressly/chi"
)
func main() {
@VojtechVitek
VojtechVitek / docker-postgres-change-owner.sh
Last active July 4, 2016 15:49
Postgres in Docker: Modify Owner of all Tables + Sequences
export new_user="your_user"
export dbname="your_db_name"
cat <<EOF | docker run -i --rm --link postgres:postgres postgres sh -c "psql -h \$POSTGRES_PORT_5432_TCP_ADDR -p \$POSTGRES_PORT_5432_TCP_PORT -U postgres -d $dbname" | grep ALTER | docker run -i --rm --link postgres:postgres postgres sh -c "psql -h \$POSTGRES_PORT_5432_TCP_ADDR -p \$POSTGRES_PORT_5432_TCP_PORT -U postgres -d $dbname"
SELECT 'ALTER TABLE '||schemaname||'.'||tablename||' OWNER TO $new_user;'
FROM pg_tables WHERE schemaname = 'public';
SELECT 'ALTER SEQUENCE '||relname||' OWNER TO $new_user;' FROM pg_class WHERE relkind = 'S';
EOF
package zlib
import (
"bytes"
"io"
"testing"
)
type zlibTest struct {
desc string
@VojtechVitek
VojtechVitek / slice-batch-in-parallel.go
Last active December 25, 2023 09:42
Golang - Loop over slice in batches (run something in parallel on a sub-slice)
package main
import "fmt"
func main() {
slice := make([]int, 159)
// Split the slice into batches of 20 items.
batch := 20
@VojtechVitek
VojtechVitek / git-checkout-pull-request.sh
Created June 19, 2017 19:59
git checkout Github Pull Request locally
git config --add remote.origin.fetch '+refs/pull/*/head:refs/remotes/origin/pr/*'
git fetch
git checkout -t origin/pr/12
# Based on https://gist.github.com/piscisaureus/3342247