Skip to content

Instantly share code, notes, and snippets.

View qbig's full-sized avatar

Liang qbig

View GitHub Profile

There are three easy to make mistakes in go. I present them here in the way they are often found in the wild, not in the way that is easiest to understand.

All three of these mistakes have been made in Kubernetes code, getting past code review at least once each that I know of.

  1. Loop variables are scoped outside the loop.

What do these lines do? Make predictions and then scroll down.

func print(pi *int) { fmt.Println(*pi) }

Keybase proof

I hereby claim:

  • I am qbig on github.
  • I am qbig (https://keybase.io/qbig) on keybase.
  • I have a public key ASAcbDSXyirF0x6Qbwpa9_2GnsP-zSlexmRBXs_HTzRoTwo

To claim this, I am signing this object:

@qbig
qbig / redis-go-sample.go
Created July 29, 2018 02:40
redis-go Client example
package cache_test
import (
"fmt"
"time"
"github.com/go-redis/redis"
"github.com/vmihailenco/msgpack"
"github.com/go-redis/cache"
package main
import "fmt"
import flag "github.com/spf13/pflag"
func main() {
var f1 *int = flag.Int("f1", 1, "help message for flagname")
var f2 *int = flag.Int("f2", 2, "help message for flagname")
flag.Parse()
fmt.Println(*f1)
@qbig
qbig / zap_example.go
Created July 29, 2018 06:18
Uber Faster logger go.uber.org/zap
// slower but easier & loosely typed interface
sugar := zap.NewExample().Sugar()
defer sugar.Sync()
sugar.Infow("failed to fetch URL",
"url", "http://example.com",
"attempt", 3,
"backoff", time.Second,
)
sugar.Infof("failed to fetch URL: %s", "http://example.com")
package main
import (
"context"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
@qbig
qbig / main_prometheus.go
Created July 29, 2018 07:50
A Simple Prometheus Server
// A minimal example of how to include Prometheus instrumentation.
package main
import (
"flag"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
@qbig
qbig / main_prometheus_sample.go
Created July 29, 2018 10:49
Uniform, normal, and exponential as Prometheus metrics.
// A simple example exposing fictional RPC latencies with different types of
// random distributions (uniform, normal, and exponential) as Prometheus
// metrics.
package main
import (
"flag"
"log"
"math"
import "github.com/grpc-ecosystem/go-grpc-middleware"
myServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_ctxtags.StreamServerInterceptor(),
grpc_opentracing.StreamServerInterceptor(),
grpc_prometheus.StreamServerInterceptor,
grpc_zap.StreamServerInterceptor(zapLogger),
grpc_auth.StreamServerInterceptor(myAuthFunction),
grpc_recovery.StreamServerInterceptor(),
@qbig
qbig / go-sql-notes.md
Last active August 5, 2018 10:07
go-sql-driver/mysql Notes

sql.DB is a abstraction of DB relation operations. It is not a connection Thus, to check if a connection is open, we need to db.Ping()

db, err := sql.Open("mysql", "user:password@/dbname")
if err != nil {
    panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
defer db.Close()

// Open doesn't open a connection. Validate DSN data: