Skip to content

Instantly share code, notes, and snippets.

@Haleluak
Haleluak / link.go
Last active December 11, 2019 06:51
This example shows how to implement an asynchronous I/O mechanism in order to reduce the number of running goroutines This allows to use a single goroutine to detect when a connection has new data that is available to read/write
@Haleluak
Haleluak / main.go
Last active October 15, 2019 04:00
Handling 1 Million Requests per Minute with Go
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"time"
@Haleluak
Haleluak / dispatcher.go
Created October 15, 2019 03:59
Handling 1 Million Requests per Minute with Go
package main
import (
"fmt"
"log"
)
type Dispatcher struct {
// A pool of workers channels that are registered with the dispatcher
maxWorkers int
@Haleluak
Haleluak / example.php
Last active September 12, 2019 09:33
Finface
<?php
define('DATEFMT', 'Y-m-d\TH:i:s.u');
/* Bounded box from "Common objects" */
class BBox {
var $x1, $y1, $x2, $y2;
function __construct($x1, $y1, $x2, $y2) {
$this->x1 = $x1;
$this->y1 = $y1;
$this->x2 = $x2;
$this->y2 = $y2;
func FindPhoneNumber(filename string) []byte {
b, _ := ioutil.ReadFile(filename)
b = regexp.MustCompile("[0–9]+").Find(b)
return append([]byte{}, b...)
}
sao chép dữ liệu cần thiết thành một slice mới nhờ hàm append, biến tham khảo tới slice cũ sẽ được giải phóng khi ra khỏi hàm.
var a []*int{...}
// phần tử cuối cùng sẽ được gán giá trị nil
// thêm phần tử 0 vào cuối slice a
a = append(a, 0)
// lùi những phần tử từ index i trở về sau
copy(a[i+1:], a[i:])
// gán vị trí thứ i bằng x
a[i] = x
// mở rộng không gian của slice a với array x
a = append(a, x...)
// sao chép len(x) phần tử lùi về sau
copy(a[i+len(x):], a[i:])
// sao chép array x vào giữa
copy(a[i:], x)
func CoverMapUrl(m map[string]string) string {
mS := ""
for k, v := range m {
mS += k + "=" + v + "&"
}
return mS[:len(mS)-1]
}
@Haleluak
Haleluak / query
Created August 27, 2019 08:19
MongoDB: Grouping by ranges using the Aggregration
db.getCollection('users').aggregate([
{
$project: {
"_id":0,
"age": {
$divide: [
{
$subtract: [
new Date(),
{ $ifNull: ["$birthday", new Date() ]}
@Haleluak
Haleluak / util
Last active August 26, 2019 03:57
func GetNamePrefix(msgName string) (prefix string) {
items := strings.Split(msgName, ".")
prefix = strings.Join(items[0:len(items)-1], ".")
return
}
func GetName(msgName string) (name string) {
items := strings.Split(msgName, ".")
name = items[len(items)-1]
return