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 / range.md
Created August 16, 2018 06:01
Learn Golang: range over array/slice/map
// _range_ iterates over elements in a variety of data
// structures. Let's see how to use `range` with some
// of the data structures we've already learned.

package main

import "fmt"

func main() {
@qbig
qbig / mysql-stmt-pipeline-util-main.go
Last active August 21, 2018 02:17 — forked from pseudomuto/main_1.go
Blog Code: Clean SQL Transactions in Golang
package main
import (
"database/sql"
"log"
)
func main() {
db, err := sql.Open("VENDOR_HERE", "YOUR_DSN_HERE")
handleError(err)
@qbig
qbig / defer.md
Last active August 17, 2018 10:18
Golang Defer is Weird

Normal case: Defer make sure something important will be called always

func CopyFile(dstName, srcName string) (written int64, err error) {
    src, err := os.Open(srcName)
    if err != nil {
        return
    }

    dst, err := os.Create(dstName)
 if err != nil {
@qbig
qbig / go-grpc-status-code.md
Created August 17, 2018 13:43
Status codes and their use in gRPC

Status codes and their use in gRPC

gRPC uses a set of well defined status codes as part of the RPC API. All RPCs started at a client return a status object composed of an integer code and a string message. The server-side can choose the status it returns for a given RPC.

The gRPC client and server-side implementations may also generate and return status on their own when errors happen. Only a subset of the pre-defined status codes are generated by the gRPC libraries. This

@qbig
qbig / new-vs-make.md
Created August 17, 2018 13:54
Golang new vs make

Allocation with new

allocate but NOT initialize--> only ZERO value, return address *T

p := new(SyncedBuffer)  // type *SyncedBuffer
var v SyncedBuffer      // type  SyncedBuffer

Allocation with make

only works for 1. slices 2. maps 3. channels

@qbig
qbig / comment.md
Created August 17, 2018 14:20
Golang Comment

Doc Comment

Inside a package, any comment immediately preceding a top-level declaration serves as a doc comment for that declaration. Every exported (capitalized) name in a program should have a doc comment.

// Compile parses a regular expression and returns, if successful,
// a Regexp that can be used to match against text.
func Compile(str string) (*Regexp, error) {

A single doc comment can introduce a group of related constants or variables.

// Error codes returned by failures to parse an expression.
@qbig
qbig / package-name.md
Created August 17, 2018 14:24
Golang Package names

package name should be considered for readability

ring.New() is better than ring.NewRing()

@qbig
qbig / getter-setter.md
Created August 17, 2018 14:26
Golang Getter and Setter

Setter is SetXXX, Getter is just XXX( NOT GetXXX)

owner := obj.Owner()
if owner != user {
    obj.SetOwner(user)
}
@qbig
qbig / interface-name.md
Created August 17, 2018 14:34
Interface names

-er suffix

By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun: Reader, Writer, Formatter, CloseNotifier etc.

There are a number of such names and it's productive to honor them and the function names they capture. Read, Write, Close, Flush, String and so on have canonical signatures and meanings. To avoid confusion, don't give your method one of those names unless it has the same signature and meaning. Conversely, if your type implements a method with the same meaning as a method on a well-known type, give it the same name and signature; call your string-converter method String not ToString.

@qbig
qbig / for.md
Created August 17, 2018 15:18
Golang for
// Like a C for
for init; condition; post { }

// Like a C while
for condition { }

// Like a C for(;;)
for { }