Skip to content

Instantly share code, notes, and snippets.

View Petelin's full-sized avatar

lin Petelin

View GitHub Profile
@Petelin
Petelin / pop_set.go
Last active June 4, 2019 09:12
pop a set
// Copyright 2013 Gary Burd
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
@Petelin
Petelin / decortor_with_recurive.go
Last active November 3, 2019 01:39
递归函数加装饰器
package main
import "fmt"
type FibI interface {
Fib(n int) int
Wrap(fib FibI) FibI
}
type Fib struct {
@Petelin
Petelin / do_pprof.go
Created November 18, 2019 11:51
上面是一个middleware实现, 最终cpu信息会记录到profMap中. (要多次调用接口才能看到效果).
func Handler(meta endpoint.Meta, next endpoint.Endpoint) endpoint.Endpoint {
profMapLock.Lock()
defer profMapLock.Unlock()
traceBytes := []byte{}
tracePtr := &traceBytes
var lock sync.Mutex
var buf bytes.Buffer
var traceBuf bytes.Buffer
@Petelin
Petelin / 4.py
Created January 6, 2020 04:18
leetcode 4.Median of Two Sorted Arrays
def median(A, B):
m, n = len(A), len(B)
if m > n:
A, B, m, n = B, A, n, m
if n == 0:
raise ValueError
imin, imax, half_len = 0, m, (m + n + 1) / 2
while imin <= imax:
i = (imin + imax) / 2
@Petelin
Petelin / condition_lock.go
Created February 28, 2020 11:06
条件锁
package versionlock
import (
"errors"
"sync/atomic"
"time"
)
var ErrOldVersion = errors.New("old version")
var ErrUnlockFailed = errors.New("unlock failed")
@Petelin
Petelin / debugPrintMiddleware.go
Created July 16, 2020 09:16
rpc 中间打印
func fieldSet(fields ...string) map[string]bool {
set := make(map[string]bool, len(fields))
for _, s := range fields {
set[s] = true
}
return set
}
func IgnoreFields(v interface{}, fields ...string) map[string]interface{} {