Skip to content

Instantly share code, notes, and snippets.

Avatar

Igor Gavrilov mytholog

View GitHub Profile
@mytholog
mytholog / letitfail.go
Created Apr 5, 2019 — forked from campoy/letitfail.go
This example shows how to have a set of goroutines running concurrently and processing requests. Panics from goroutines are recovered and the worker is restarted. You can download it and run it directly using `go run letitfail.go`
View letitfail.go
package main
import (
"bufio"
"fmt"
"os"
"time"
)
const numWorkers = 3
@mytholog
mytholog / worker_pool.go
Created Aug 13, 2018
pool of go routines
View worker_pool.go
package main
import (
"fmt"
"sync"
"time"
)
/*
In many languages, you need a "pool" mechanism to handle threads, because
@mytholog
mytholog / postgresql-set-id-seq.sql
Created Apr 26, 2018 — forked from henriquemenezes/postgresql-set-id-seq.sql
PostgreSQL set Next ID Sequence Value to MAX(id) from Table
View postgresql-set-id-seq.sql
-- Get Max ID from table
SELECT MAX(id) FROM table;
-- Get Next ID from table
SELECT nextval('table_id_seq');
-- Set Next ID Value to MAX ID
SELECT setval('table_id_seq', (SELECT MAX(id) FROM table)+1);
@mytholog
mytholog / stack_queue.go
Last active Sep 13, 2017 — forked from moraes/gist:2141121
LIFO Stack and FIFO Queue in golang
View stack_queue.go
package main
import (
"fmt"
)
type Node struct {
Value int
}
View revers.go
func reverse(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func reverseString(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
View binarySearch.go
func binarySearch(a []int, search int) (result int, searchCount int) {
mid := len(a) / 2
switch {
case len(a) == 0:
result = -1 // not found
case a[mid] > search:
result, searchCount = binarySearch(a[:mid], search)
case a[mid] < search:
result, searchCount = binarySearch(a[mid+1:], search)
result += mid + 1
View bubbleSort.go
tmp := 0
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr) - 1; j++ {
if arr[j] > arr[j + 1] {
tmp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = tmp
}
@mytholog
mytholog / StreamToString.go
Created Jul 5, 2017 — forked from tejainece/StreamToString.go
Golang: io.Reader stream to string or byte slice
View StreamToString.go
import "bytes"
func StreamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
func StreamToString(stream io.Reader) string {
buf := new(bytes.Buffer)
@mytholog
mytholog / closest.js
Created Mar 6, 2017
closest number out of array
View closest.js
const counts = [4, 9, 15, 6, 2];
const goal = 5;
counts
.reduce((prev, curr) => Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
@mytholog
mytholog / deepClone.js
Created Feb 22, 2017
deep clone JS object
View deepClone.js
function deepClone(obj) {
var copy;
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
copy = new Date();
copy.setTime(obj.getTime());