Skip to content

Instantly share code, notes, and snippets.

View YanhaoYang's full-sized avatar

Yanhao Yang YanhaoYang

  • Berlin, Germany
View GitHub Profile
@YanhaoYang
YanhaoYang / lazy-ruby.rb
Created March 16, 2017 08:10
lazy ruby calls each block sequentially
irb(main):001:0> 1.upto(Float::INFINITY).lazy.
irb(main):002:0* map { |x| puts "1 #{x}"; x * x }.
irb(main):003:0* map { |x| puts "2 #{x}"; x + 1 }.
irb(main):004:0* take(10).to_a
1 1
2 1
1 2
2 4
1 3
2 9
@YanhaoYang
YanhaoYang / kubectl-port-forward.sh
Created April 10, 2017 03:03
Generate `kubectl port-forward` cmds
#!/bin/bash
pods=($(kubectl get po | grep -E 'api|db' | awk '{print $1}'))
for var in "${pods[@]}"
do
case "$var" in
*api*)
api_cmd="kubectl port-forward $var 3000:3000"
;;
@YanhaoYang
YanhaoYang / go-sql-in-query.go
Created April 28, 2017 08:40
SQL's IN query in Golang
func updateAll(ids []string) {
var params []string
var values []interface{}
for k, v := range ids {
params = append(params, fmt.Sprintf("$%d", k+1))
values = append(values, v)
}
db := viper.Get("db").(*gorp.DbMap)
sql := "UPDATE stores SET active = false WHERE id IN (" + strings.Join(params, ",") + ")"
rs, err := db.Exec(sql, values...)
@YanhaoYang
YanhaoYang / named-errors-in-golang.go
Last active May 11, 2017 05:47
Handle named errors with switch-case
// BaseError ...
type BaseError struct {
s string
}
func (e *BaseError) Error() string {
return e.s
}
// SomeError ...
@YanhaoYang
YanhaoYang / plpgsql-update-in-batch.sql
Created June 23, 2017 02:13
plpgsql-update-in-batch
DO $$DECLARE quantity integer := 1; maxid integer; cnt integer; total integer := 0;
BEGIN
SELECT max(id) INTO maxid FROM google_places;
maxid := maxid + 10;
RAISE NOTICE 'maxid: %', maxid;
LOOP
UPDATE some_table t1
SET some_column = ...
FROM another_table t2
WHERE ...
@YanhaoYang
YanhaoYang / golang-struct-copy-is-memory-copy.go
Created June 25, 2017 14:57
golang-struct-copy-is-memory-copy
package main
import (
"fmt"
)
type A struct {
S1 string
}
@YanhaoYang
YanhaoYang / bm-slice.go
Created June 27, 2017 09:38
[Golang] make a slice and populate it by index or with `append`?
package main
import (
"fmt"
"testing"
)
func BenchmarkAppendFloat(b *testing.B) {
benchmarks := []int{10, 100, 1000, 10000, 1e5}
for _, bm := range benchmarks {
@YanhaoYang
YanhaoYang / golang-polymorphism.go
Created July 5, 2017 07:22
golang-polymorphism
// https://play.golang.org/p/CpPW7CxYBV
package main
import (
"fmt"
)
type A struct {
X string
@YanhaoYang
YanhaoYang / rc.local
Created July 14, 2017 01:30
/etc/rc.local for Mint on Mac
# fix keyboard
echo 0 > /sys/module/hid_apple/parameters/iso_layout
# prevent wakeup from usb
echo XHC1 > /proc/acpi/wakeup
exit 0
@YanhaoYang
YanhaoYang / array-of-different-types.go
Created August 23, 2017 10:28
Array of different types
package main
import (
"fmt"
)
type A struct{}
func (A) hi() string {
return "hi A"