Skip to content

Instantly share code, notes, and snippets.

View pedrobertao's full-sized avatar
🚀

Pedro Bertao pedrobertao

🚀
View GitHub Profile
@pedrobertao
pedrobertao / gqlRawReq.go
Last active September 17, 2022 11:58
Raw HTTP request to a GraphQL server
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"time"
package main
import (
"log"
"reflect"
"os"
)
// Find an object in a slice comparing a KEY (string) and its VALUE (interface{})
func findByKey(objArr interface{}, key string, value interface{}) interface{} {
package main
import "fmt"
type Person struct {
Name string
Surname string
}
func main() {
/*
How to use context with timeout to cancel a slow operation.
The same concept can be used with context.WithDeadline()
*/
package main
import (
"context"
"fmt"
"time"
@pedrobertao
pedrobertao / keybase.md
Last active January 14, 2021 21:34
keybase.md

Keybase proof

I hereby claim:

  • I am pedrobertao on github.
  • I am bertao (https://keybase.io/bertao) on keybase.
  • I have a public key ASCDjda7PSWYI3xKQHFGe-xZzoOQ1UXcDGZrQO2AvZNaaQo

To claim this, I am signing this object:

@pedrobertao
pedrobertao / retryclosure.go
Created January 15, 2021 00:43
Function retrier using closure
/*
Retry function using closure
*/
package main
import (
"errors"
"fmt"
"time"
)
@pedrobertao
pedrobertao / any.go
Last active July 10, 2023 00:06
This is a simple example to use ANY in golang which is an alias for interface{}, therefore easy to read
#################################################
## EXAMPLE OF USAGE OF ANY IN AN OBJECT/STRUCT ##
#################################################
package main
import "fmt"
const (
MALE string = "male"
FEMALE string = "female"
// Range between two int64
func randomNum(min, max int64) int64 {
rand.Seed(time.Now().UnixNano())
num := (rand.Int63n(max-min+1) + min)
return num
}
func randomNum(min, max int64) string {
@pedrobertao
pedrobertao / min-max.go
Last active August 27, 2023 12:16
Min/Max Golang 1.21
package main
import "fmt"
func main() {
minInt := min(3, 2, 1, 4)
minFloat := min(4.0, 2.0, 3.0, 1.0)
minString := min("ab", "a", "abcd", "abc")
@pedrobertao
pedrobertao / clear.go
Created August 26, 2023 10:39
Example of Clear in Go
package main
import "fmt"
func main() {
intSlice := []int{1, 2, 3}
floatSlice := []float64{1.0, 2.0, 3.0}
stringSlice := []string{"a", "b", "c"}
mapString := map[string]string{
"Name": "Pedro",