Skip to content

Instantly share code, notes, and snippets.

View BruceChen7's full-sized avatar
🎯
Focusing

insects BruceChen7

🎯
Focusing
View GitHub Profile
@BruceChen7
BruceChen7 / mock.go
Created April 15, 2022 07:08
#mock#golang#arch
package repository_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
@BruceChen7
BruceChen7 / function.go
Last active April 14, 2022 10:42
#golang#function#high-order
// 高阶函数
func multiply(a int) func(int) int {
return func(i int) int {
return a * i
}
}
func subtract(a int) func(int) int {
return func(i int) int {
return i - a
}
@BruceChen7
BruceChen7 / group.go
Last active April 14, 2022 07:26
#golang#sync#waitgroup
var (
wg sync.WaitGroup
seriesSetChan = make(chan genericSeriesSet)
)
// Schedule all Selects for all queriers we know about.
for _, querier := range q.queriers {
wg.Add(1)
go func(qr genericQuerier) {
defer wg.Done()
@BruceChen7
BruceChen7 / error.go
Created April 14, 2022 06:39
#golang#error
// multiError type allows combining multiple errors into one.
type multiError []error
// NewMulti returns multiError with provided errors added if not nil.
func NewMulti(errs ...error) multiError { // nolint:revive
m := multiError{}
// 多个error的定义
m.Add(errs...)
return m
}
@BruceChen7
BruceChen7 / file.go
Created April 14, 2022 06:20
#file#directory#os
// CopyDirs copies all directories, subdirectories and files recursively including the empty folders.
// Source and destination must be full paths.
func CopyDirs(src, dest string) error {
if err := os.MkdirAll(dest, 0o777); err != nil {
return err
}
// 获取所有的文件
files, err := readDirs(src)
if err != nil {
return err
@BruceChen7
BruceChen7 / gc.go
Created April 13, 2022 16:27
#golang#gc#memory
// go build -gcflags="-m"`
// 可以进行逃逸分析
package main
func main() {
var m = make([]int, 10240)
println(m[0])
}
@BruceChen7
BruceChen7 / buffer.lua
Last active April 11, 2022 04:41
#neovim#buffer
-- https://github.com/TimUntersberger/neogit/blob/master/lua/neogit/lib/buffer.lua)
package.loaded['neogit.buffer'] = nil
__BUFFER_AUTOCMD_STORE = {}
local mappings_manager = require("neogit.lib.mappings_manager")
local Ui = require("neogit.lib.ui")
local Buffer = {
handle = nil,
@BruceChen7
BruceChen7 / dop.js
Last active April 20, 2022 14:59
#dop#
var catalogData = {
"booksByIsbn": {
"978-1779501127": {
"isbn": "978-1779501127",
"title": "Watchmen",
"publicationYear": 1987,
"authorIds": ["alan-moore", "dave-gibbons"],
"bookItems": [{
"id": "book-item-1",
"libId": "nyc-central-lib",
@BruceChen7
BruceChen7 / await.rs
Last active April 2, 2022 15:11
#rust#async#await
/// https://rust-lang.github.io/async-book/04_pinning/01_chapter.html
/// https://rust-lang.github.io/async-book/04_pinning/01_chapter.html
/// Task executor that receives tasks off of a channel and runs them.
struct Executor {
ready_queue: Receiver<Arc<Task>>,
}
/// `Spawner` spawns new futures onto the task channel.
#[derive(Clone)]
struct Spawner {
@BruceChen7
BruceChen7 / await.rs
Created April 2, 2022 13:53
#rust#async#await
impl Executor {
fn run(&self) {
while let Ok(task) = self.ready_queue.recv() {
// Take the future, and if it has not yet completed (is still Some),
// poll it in an attempt to complete it.
let mut future_slot = task.future.lock().unwrap();
if let Some(mut future) = future_slot.take() {
// Create a `LocalWaker` from the task itself
let waker = waker_ref(&task);
let context = &mut Context::from_waker(&*waker);