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 / 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 / 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
$ eksctl create cluster -v 4 --name=dev3 --nodes-min=2 --nodes-max=10
2018-12-21T16:54:51Z [ℹ] using region us-west-2
2018-12-21T16:54:52Z [▶] role ARN for the current session is "arn:aws:iam::797098425712:user/vojtech"
2018-12-21T16:54:52Z [▶] determining availability zones
2018-12-21T16:54:52Z [ℹ] setting availability zones to [us-west-2c us-west-2a us-west-2b]
2018-12-21T16:54:52Z [▶] VPC CIDR (192.168.0.0/16) was divided into 8 subnets [192.168.0.0/19 192.168.32.0/19 192.168.64.0/19 192.168.96.0/19 192.168.128.0/19 192.168.160.0/19 192.168.192.0/19 192.168.224.0/19]
2018-12-21T16:54:52Z [ℹ] subnets for us-west-2c - public:192.168.0.0/19 private:192.168.96.0/19
2018-12-21T16:54:52Z [ℹ] subnets for us-west-2a - public:192.168.32.0/19 private:192.168.128.0/19
2018-12-21T16:54:52Z [ℹ] subnets for us-west-2b - public:192.168.64.0/19 private:192.168.160.0/19
2018-12-21T16:54:52Z [▶] resolving AMI using StaticGPUResolver for region us-west-2, instanceType m5.large and imageFamily AmazonLinux2
{"AWSTemplateFormatVersion":"2010-09-09","Description":"EKS cluster (dedicated VPC: true, dedicated IAM: true) [created and managed by eksctl]","Resources":{"ControlPlane":{"Type":"AWS::EKS::Cluster","Properties":{"Name":"dev3","ResourcesVpcConfig":{"SecurityGroupIds":[{"Ref":"ControlPlaneSecurityGroup"}],"SubnetIds":[{"Ref":"SubnetPublicUSWEST2C"},{"Ref":"SubnetPublicUSWEST2A"},{"Ref":"SubnetPublicUSWEST2B"},{"Ref":"SubnetPrivateUSWEST2C"},{"Ref":"SubnetPrivateUSWEST2A"},{"Ref":"SubnetPrivateUSWEST2B"}]},"RoleArn":{"Fn::GetAtt":"ServiceRole.Arn"},"Version":"1.11"}},"ControlPlaneSecurityGroup":{"Type":"AWS::EC2::SecurityGroup","Properties":{"GroupDescription":"Communication between the control plane and worker node groups","Tags":[{"Key":"Name","Value":{"Fn::Sub":"${AWS::StackName}/ControlPlaneSecurityGroup"}}],"VpcId":{"Ref":"VPC"}}},"InternetGateway":{"Type":"AWS::EC2::InternetGateway","Properties":{"Tags":[{"Key":"Name","Value":{"Fn::Sub":"${AWS::StackName}/InternetGateway"}}]}},"NATGateway":{"Type":"AWS::
@VojtechVitek
VojtechVitek / try-error-check.go
Created June 5, 2019 09:37
Proposal: try, a built-in Go error check function
func CopyFile(src, dst string) (err error) {
defer fmt.HandleErrorf(&err, "copy %s %s: %v", src, dst, err)
r := try(os.Open(src))
defer r.Close()
w := try(os.Create(dst))
defer func() {
w.Close()
if err != nil {