Skip to content

Instantly share code, notes, and snippets.

@Haleluak
Haleluak / request.go
Last active June 19, 2020 06:57
make request multipart
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
@Haleluak
Haleluak / ssd.py
Created June 10, 2020 04:37
Social Distancing Detector
def Check(a, b):
dist = ((a[0] - b[0]) ** 2 + 550 / ((a[1] + b[1]) / 2) * (a[1] - b[1]) ** 2) ** 0.5
calibration = (a[1] + b[1]) / 2
if 0 < dist < 0.25 * calibration:
return True
else:
return False
@Haleluak
Haleluak / sse.go
Created May 27, 2020 09:05 — forked from ismasan/sse.go
Example SSE server in Golang
// Copyright (c) 2017 Ismael Celis
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
@Haleluak
Haleluak / mongo
Created February 18, 2020 04:00
pagingnator
Using this property of ObjectId and also taking into consideration the fact that _id is always indexed, we can devise following approach for pagination:
Fetch a page of documents from database
Get the document id of the last document of the page
Retrieve documents greater than that id
In Mongo Shell your pagination code looks something like this
// Page 1
db.students.find().limit(10)
@Haleluak
Haleluak / note.go
Created February 17, 2020 07:15
note
Lưu ý khi return không nên return biến pointer bởi lẽ hệ thống phải tạo lại biến pointer mới rồi copy dữ liệu biến con trỏ cần return trỏ tới chứ không lấy luôn được con trỏ return này đâu do sẽ bị hủy sau khi return xong.
@Haleluak
Haleluak / worker_pool.go
Last active December 17, 2019 06:26
worker_pool
package main
import (
"fmt"
"time"
)
const MaxGoroutines = 5
type WorkerPool struct {
@Haleluak
Haleluak / parallel-fn.go
Created December 17, 2019 03:55
Run functions in parallel
package parallel
import (
"sync"
)
// Func is the function to run concurrently.
type Func func() error
// Run calls the passed functions in a goroutine, returns a chan of errors.
@Haleluak
Haleluak / merge.go
Last active December 9, 2019 08:36
merging n channels
func merge(cs ...<-chan int) <-chan int {
out := make(chan int)
var wg sync.WaitGroup
wg.Add(len(cs))
for _, c := range cs {
go func(c <-chan int) {
for v := range c {
out <- v
}
wg.Done()
@Haleluak
Haleluak / query
Created October 30, 2019 10:09
Get the Most Recent Item in a Group
{ "_id": 1, "color": "red", "date": ISODate("2013-05-10T00:00:00Z") }
{ "_id": 2, "color": "blue", "date": ISODate("2016-10-20T00:00:00Z") }
{ "_id": 3, "color": "green", "date": ISODate("2015-09-14T00:00:00Z") }
{ "_id": 4, "color": "green", "date": ISODate("2015-07-19T00:00:00Z") }
{ "_id": 5, "color": "red", "date": ISODate("2017-01-04T00:00:00Z") }
{ "_id": 6, "color": "blue", "date": ISODate("2014-03-06T00:00:00Z") }
result = db.books.aggregate([
{ $sort: { "date": -1 } },
{ $group: {
@Haleluak
Haleluak / golang_job_queue.md
Created October 23, 2019 06:40 — forked from harlow/golang_job_queue.md
Job queues in Golang