西维蜀黍

【Golang】使用 - sync.Mutex

This concept is called mutual exclusion, and the conventional name for the data structure that provides it is mutex.

type Mutex

A Mutex is a mutual exclusion lock(排它锁). The zero value for a Mutex is an unlocked mutex.

A Mutex must not be copied after first use.

type Mutex struct {
    // contains filtered or unexported fields
}
  ...


【Golang】Panic and Recover

  ...


【Golang】Exit Code

os.Exit()

Use os.Exit to immediately exit with a given status.

package main

import (
    "fmt"
    "os"
)

func main() {
    defer fmt.Println("!") // never called

    os.Exit(3)
}
  • Conventionally, code zero indicates success, non-zero an error
  • defers will not be run when using os.Exit, so this fmt.Println will never be called.
  • For portability, the status code should be in the range [0, 125].
  ...


【Golang】使用 - context

Background

Many people who have worked with Go, would have encountered it’s context library. Most use context with downstream operations, like making an HTTP call, or fetching data from a database, or while performing async operations with go-routines. It’s most common use is to pass down common data which can be used by all downstream operations. However, a lesser known, but highly useful feature of context is it’s ability to cancel, or halt an operation mid-way.

This post will explain how we can make use of the context libraries cancellation features, and go through some patterns and best practices of using cancellation to make your application faster and more robust.

  ...


【Python】SimpleHTTPServer

Python2

$ python -m SimpleHTTPServer
# or
$ python2 -m SimpleHTTPServer

Python3

$ python3 -m http.server
  ...