Skip to content

Instantly share code, notes, and snippets.

View davidlares's full-sized avatar
🎯
Focusing

David E Lares S davidlares

🎯
Focusing
View GitHub Profile
@davidlares
davidlares / dns.py
Last active July 19, 2021 03:13
Dnspython library overview
#!/usr/bin/python
import dns.resolver
import dns.zone # XFR (Transfer zone requests) TCP53
import dns.name # domain lookup
import dns.reversename # reverse DNS lookup
def print_records(data, type):
print("*" * 10)
print("Record %s" % type.upper())
@davidlares
davidlares / context.md
Last active July 14, 2021 02:00
Custom HTTP server generation and contexts with Golang

Context package

The context type in Golang carries deadlines, cancelation signals, and other request-scoped values across API boundaries and between system processes

Here's a simple example

  ctx := context.Background()
  context.WithCancel(ctx)
@davidlares
davidlares / client.go
Last active July 14, 2021 01:36
Raw response POST Request evaluation
package main
import (
"io/ioutil"
"net/http"
"net/url"
"bytes"
"fmt"
"log"
)
@davidlares
davidlares / client.go
Last active July 14, 2021 01:33
Raw response GET Request evaluation
package main
import (
"io/ioutil"
"net/http"
"fmt"
"log"
)
func main() {
@davidlares
davidlares / mux-http-with-params.go
Last active July 14, 2021 00:47
Golang's Mux HTTP GET request with params
package main
import (
"github.com/gorilla/mux"
"net/http"
"fmt"
)
func main() {
// new mux router
@davidlares
davidlares / raw-http.go
Last active July 14, 2021 00:44
Raw HTTP packet digest with Golang
package main
import (
"io/ioutil"
"net/http"
"strings"
"fmt"
"log"
)
@davidlares
davidlares / channels-goroutines.md
Last active July 13, 2021 17:53
Golang's channels and Go-routines

Channels and Go-routines

Both mechanisms are meant for concurrent programming. How to work with concurrency in Go.

When you run a program in Golang, automatically generates a Go-routine, this is the engine that executes code. The go keyword in keywords runs this function into a new Go-routine, in a general manner, some sort of parallelism is made, it avoids blocking calls.

Things change when there's only one CPU. The Go Scheduler can execute many Go-routines but actually runs one routine at a time until it's finished or makes a blocking call, so that 'parallelism' is not fully presented.

If your server has many CPUs, each thread will run on each logical core. However, by default, Go tries to use One core.

@davidlares
davidlares / byte-slice.go
Created July 13, 2021 16:21
Byte-slice in Golang
package main
import (
"fmt"
)
func main() {
// byte slice = computer friendly representation of a string
str := []byte("Hello slice")
fmt.Println(str)
@davidlares
davidlares / oop-golang.md
Last active July 13, 2021 06:49
The 'OOP' perspective in Golang

The OOP perspective in Golang

At a certain moment, when working with Golang we mentally start thinking about classes and methods, just the regular OOP way of working.

In the OOP approach, a Struct data type can be seen as a Class definition, the Blueprint for incoming objects, where, variables or even arrays can be seen as attributes and some functions can be seen as methods.

But that's not all correct.

The ideal scenario here is that we need a way to extending a base type to add some extra functionality. By base types, I'm referring to strings, integers, arrays, maps, and so on. Any custom method that can be appliable to an instance, we can 'associate' it to an OOP object.

@davidlares
davidlares / maps.go
Last active July 13, 2021 16:09
Maps in Golang
package main
import "fmt"
// Map
func main() {
// statically type [key]values{}
colors := map[string]string{
"red": "#ff0000",