Skip to content

Instantly share code, notes, and snippets.

@tailnode
tailnode / monty_hall.go
Created February 5, 2019 02:56
monty_hall
package main
import (
"fmt"
"math/rand"
"time"
)
const doorCount = 3
const (
package utils
import (
"sync"
)
type resourceSet struct {
sync.Map
mapLock sync.Mutex
locks map[interface{}]*sync.Mutex
@tailnode
tailnode / range_slice_map.go
Created January 1, 2018 13:34
range slice and map
package main
import (
"fmt"
"sync"
)
func main() {
testSlice()
testMap()
@tailnode
tailnode / m_sender_n_receiver.go
Created January 1, 2018 03:22
stop M sender N receiver using one channel
package main
import (
"fmt"
"math/rand"
"time"
)
const senderNum = 20
const receiverNum = 7
@tailnode
tailnode / do_with_timeout.go
Created December 31, 2017 02:04
do with timeout in go
package main
import (
"errors"
"fmt"
"time"
)
var timeoutErr = errors.New("timeout")
// 比较不同情况下不同字符串拼接方法的效率
// 测试函数使用 []string 只是为了方便,在好多情况下要拼接的字符不会在同一个 slice 中
package main
import (
"bytes"
"fmt"
"strings"
"testing"
)
// 测试 RoundRobin 中通过取余和通过判断大小的两种方法的性能
package main
import "testing"
type roundrobin struct {
i int // 服务地址索引
servers []string // 可用的服务地址
}
@tailnode
tailnode / trimJson.go
Created April 13, 2017 06:38
golang 将复杂 struct 输出为 JSON 时过滤掉不需要字段(不修改原 struct)
package main
import (
"encoding/json"
"fmt"
)
type T1 struct {
Field1 string `json:"field_1"`
Field2 []int `json:"field_2"`
@tailnode
tailnode / 佛祖保佑,哈哈
Created December 24, 2014 02:00
佛祖保佑,永不宕机,永无bug
//
// _oo8oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/'==='\___
// .' \\| |// '.
// / \\||| : |||// \
// / _||||| -:- |||||_ \
@tailnode
tailnode / 颠倒各个位顺序
Created November 26, 2014 10:45
按二进制格式颠倒各个位顺序:0xAA(10101010) -> 0x55(0x01010101)
unsigned int revert(unsigned char data)
{
const unsigned char table[] = {0, 2, 1, 3};
unsigned int revert = (table[data >> 6]) | (table[(data & 0x30) >> 4] << 2)
| (table[(data & 0x0c) >> 2] << 4) | (table[data & 0x03] << 6);
return revert;
}