Skip to content

Instantly share code, notes, and snippets.

View qbig's full-sized avatar

Liang qbig

  • @us3r-network
  • Singapore
View GitHub Profile
@qbig
qbig / go-sql-common-pitfalls.md
Last active August 5, 2018 10:14
Go SQL driver common pitfalls

Deferring inside a loop. A long-lived function with a query inside a loop, and defer rows.Close() inside the loop, will cause both memory and connection usage to grow without bounds.

Opening many db objects. Make a global sql.DB, and don’t open a new one for, say, every incoming HTTP request your API server should respond to. Otherwise you’ll be opening and closing lots of TCP connections to the database. It’s a lot of latency, load, and TCP connections in TIME_WAIT status.

@qbig
qbig / nil-vs-closed-channels.md
Last active August 10, 2018 02:16
Go Channel Axiom (Nil is nice, Close is nasty)

Channel Axioms

A send to a nil channel blocks forever A receive from a nil channel blocks forever

A send to a closed channel panics A receive from a closed channel returns the zero value immediately

Check if a channel is closed

@qbig
qbig / go-concurrency.md
Last active August 7, 2018 02:29
Go Concurrency Patterns

Pipeline

func gen(nums ...int) <-chan int {
    out := make(chan int)
    go func() {
        for _, n := range nums {
            out <- n
        }
        close(out)
    }()
@qbig
qbig / grpc-basic.md
Created August 8, 2018 11:05
GRPC boilerplate

Server

flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
        log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
pb.RegisterRouteGuideServer(grpcServer, &routeGuideServer{})
// ... // determine whether to use TLS
@qbig
qbig / panic.md
Created August 10, 2018 02:37
Panic and recover
func server(workChan <-chan *Work) {
    for work := range workChan {
        go safelyDo(work)
    }
}

func safelyDo(work *Work) {
    defer func() {
 if err := recover(); err != nil {
@qbig
qbig / io-interfaces.md
Created August 14, 2018 12:26
Using the common I/O interfaces

The io.Reader and io.Writer interfaces

type Reader interface {
        Read(p []byte) (n int, err error)
}

type Writer interface {
        Write(p []byte) (n int, err error)
}
@qbig
qbig / bytes-and-strings-pkg.md
Created August 14, 2018 12:49
Using the bytes and strings packages

Working with buffer

        package bytestrings

        import (
                "bytes"
                "io"
                "io/ioutil"
        )
@qbig
qbig / filedir.md
Created August 14, 2018 13:08
Working with directories and files

Working with dir

 package filedirs

        import (
                "errors"
                "io"
                "os"
        )
@qbig
qbig / csv.md
Created August 14, 2018 13:21
Working with the CSV format

Reading CSV

  package csvformat

        import (
                "bytes"
                "encoding/csv"
                "fmt"
                "io"
                "strconv"
@qbig
qbig / html-template.md
Created August 14, 2018 13:31
Working with text/template and HTML/templates

Working with template

 package templates

        import (
                "os"
                "strings"
                "text/template"
        )