Skip to content

Instantly share code, notes, and snippets.

View crgimenes's full-sized avatar
🏠
Working from home

Cesar Gimenes crgimenes

🏠
Working from home
View GitHub Profile
@crgimenes
crgimenes / util.go
Created January 18, 2017 10:39
create directory if not exist, also create all missing tree entries
func createDirIfNotExist(pathName string) (err error) {
if _, err = os.Stat(pathName); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(pathName, os.ModePerm)
return
}
}
return
}
@crgimenes
crgimenes / util.go
Last active January 25, 2017 08:39
Create random string with pre-defined size.
import "crypto/rand"
func RandStr(strSize int) (ret string, err error) {
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, strSize)
_, err = rand.Read(bytes)
if err != nil {
return
}
for k, v := range bytes {
@crgimenes
crgimenes / readFromStdin.go
Created February 2, 2017 12:07
Simplest way to read from stdin
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
input, err := ioutil.ReadAll(os.Stdin)
@crgimenes
crgimenes / client.go
Created April 26, 2017 22:09 — forked from spikebike/client.go
TLS server and client
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log"
)
@crgimenes
crgimenes / parsearray.go
Last active June 1, 2017 14:24
Experimental function to parse array/slice to string compatible with array of postgresql
func parseArray(value interface{}) string {
switch value.(type) {
case []interface{}:
var aux string
for _, v := range value.([]interface{}) {
if aux != "" {
aux += ","
}
aux += parseArray(v)
}
@crgimenes
crgimenes / findSubmatch.go
Created June 12, 2017 12:15
Simple regex submatch function example
func FindSubmatch(text string, regex string, submatch int) (ret string) {
var r = regexp.MustCompile(regex)
a := r.FindStringSubmatch(text)
if len(a) <= submatch {
ret = ""
return
}
ret = a[submatch]
ret = strings.TrimSpace(ret)
return
@crgimenes
crgimenes / memoizedFibonacci.go
Created February 4, 2018 14:05
Memoized fibonacci from `Learning Functional Programming in Go`
package fibonacci
type Memoized func(int) int
var fibMem = Memoize(fib)
func Memoize(mf Memoized) Memoized {
cache := make(map[int]int)
return func(key int) int {
if val, found := cache[key]; found {
return val
@crgimenes
crgimenes / TestHTTPHandler.go
Created February 22, 2018 13:17
test httphandler example
func TestHTTPHandler(t *testing.T) {
w := httptest.NewRecorder()
expected := "{\n\t\"error\": \"test error\"\n}\n"
HTTPHandler(w, nil)
b := w.Body.Bytes()
if string(b) != expected {
t.Errorf("expected %q, want %q", expected, string(b))
@crgimenes
crgimenes / closer.go
Created February 28, 2018 21:10
function to close descriptor using defer without ignore error
func closer(body io.Closer) {
err := body.Close()
if err != nil {
log.Errorln(err)
}
}
// defer closer(fileDescriptor)
@crgimenes
crgimenes / SeekingBuffer.go
Created April 13, 2018 00:56
SeekingBuffer example
// https://play.golang.org/p/8SjYn-cisj
package main
import (
"bytes"
"fmt"
"io"
"os"
)