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 / 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"
)
@crgimenes
crgimenes / execHelper.go
Created April 20, 2018 12:45
simple exec example
func execHelper(path, name string, arg ...string) (err error) {
cmd := exec.Command(name, arg...)
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
return
}
@crgimenes
crgimenes / closer.go
Created June 14, 2018 19:11
closer and tests
package helper
// closer close descriptor and log error
func closer(f io.Closer) {
err := f.Close()
if err != nil {
log.Errorln("closing ", err)
}
}
@crgimenes
crgimenes / draftFilesystem.go
Created July 13, 2018 00:38
simple filesystem example
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"syscall"
@crgimenes
crgimenes / tempFileName.go
Created August 3, 2018 19:54
Filename for temp files
func newFileName() string {
buff := make([]byte, 6)
rand.Read(buff)
return fmt.Sprintf("%v-%X\n", time.Now().UTC().Format("2006-01-02T150405"), buff)
}
@crgimenes
crgimenes / README.md
Last active January 5, 2025 12:08
Example of pagination using PostgreSQL, Golang and SQLx

Configure environment variable

export DATABASE_URL=postgres://postgres@localhost/dbname?sslmode=disable 

Run in CLI

go run main.go -page 1
@crgimenes
crgimenes / strarray.go
Last active December 14, 2023 21:17
Golang Example of Receiving Arrays or Strings in Same JSON Object
package strarray
import (
"encoding/json"
"errors"
)
var (
// ErrUnsupportedType is returned if the type is not implemented
ErrUnsupportedType = errors.New("unsupported type")
@crgimenes
crgimenes / main.go
Last active February 17, 2025 23:37
Web Scraping with Golang
package main
import (
"context"
"log"
"github.com/chromedp/chromedp"
)
func main() {
@crgimenes
crgimenes / main.go
Created May 21, 2020 21:11
Detect server and client ip and port
package main
import (
"fmt"
"net"
"net/http"
"github.com/gorilla/mux"
)