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 / 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 / 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 / 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 / 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 / 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 / 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 / 1. alloc.go
Last active March 15, 2017 07:58
Golang memory allocation and GC -- array + join is much better than string + string + string ...
package main
import (
"fmt"
"runtime"
)
func main() {
m := 1
s := ""
@YanhaoYang
YanhaoYang / Static Website Hosting.md
Created February 28, 2017 02:57
AWS S3 Static Website Hosting with redirection

Redirection Rules

Suppose there are two buckets: one-bucket, another-bucket.

Show content in another bucket when the file is missing in one bucket:

  <RoutingRules>
    <RoutingRule>
    <Condition>
      <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals >
@YanhaoYang
YanhaoYang / callerinfo.go
Created February 15, 2017 06:37
Print caller info
package main
import (
"fmt"
"path"
"runtime"
)
func callerInfo() string {
function, file, line, _ := runtime.Caller(0)
@YanhaoYang
YanhaoYang / closable.go
Created February 9, 2017 07:46
Solution: error return value not checked (defer response.Body.Close())
// Closable wraps Close() method
type Closable interface {
Close() error
}
func closeIt(obj Closable) {
err := obj.Close()
if err != nil {
log.Fatal(err)
}