Skip to content

Instantly share code, notes, and snippets.

View Tanmay451's full-sized avatar

Tanmay Piyush Kaushik Tanmay451

View GitHub Profile
@Tanmay451
Tanmay451 / decorator_pattern.go
Last active December 11, 2023 11:39
Decorator pattern in Go (golang)
/*
The provided Go code demonstrates the use of a decorator pattern to implement authorization checks for HTTP endpoints. The main components of the code include:
isAuthorized Function:
It takes an HTTP handler function (endpoint) as a parameter and returns an http.Handler.
The returned http.Handler is a function that checks for the presence of an "Authorized" header in the incoming HTTP request.
If the header is present and set to "true," the original endpoint function is called, allowing the request to proceed.
If the header is not present or set to a value other than "true," a "Not Authorized" message is sent as the response.
homePage Function:
This is a sample HTTP handler function representing the home page.
@Tanmay451
Tanmay451 / dns_resolution.sh
Created January 5, 2024 10:07
The output traces the process of resolving the IP address for "google.com" through multiple DNS servers. It demonstrates how DNS queries are delegated to different servers within the DNS hierarchy until the authoritative answer is found.
# https://www.iana.org/domains/root/servers
# https://blog.bytebytego.com/p/why-the-internet-is-both-robust-and
# Query sent to a root nameserver (198.41.0.4).
# Root server doesn't have the answer for "google.com", but provides a referral to the .com TLD nameservers.
# Response includes a list of .com TLD nameservers (e.g., e.gtld-servers.net, b.gtld-servers.net).
➜ ~ dig @198.41.0.4 google.com
; <<>> DiG 9.10.6 <<>> @198.41.0.4 google.com
; (1 server found)
@Tanmay451
Tanmay451 / generic.go
Created January 10, 2024 10:33
Generics, also known as parametric polymorphism, enable you to write code that operates on multiple types without explicitly specifying the types upfront. This leads to more concise, reusable, and type-safe code.
// In the PrintSlice function, T is a type parameter, and any is a type constraint.
// The any constraint is built into the language and allows any type to be used.
// The function can be called with any slice type, such as []int or []string.
package main
import "fmt"
// PrintSlice prints each element of a slice of any type.
func PrintSlice[T any](s []T) {
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"time"
// https://gist.github.com/hongster/04660a20f2498fb7b680
// https://github.com/arpitbbhayani/tcpserver/blob/master/main.go
package main
import (
"fmt"
"log"
"net"
"time"
@Tanmay451
Tanmay451 / first-class_and_higher-order_functions.go
Last active May 21, 2024 14:09
In Go, functions are first-class citizens. This means they can be assigned to variables, passed as arguments to other functions, and returned from functions. Higher-order functions are functions that take other functions as parameters or return functions.
package main
import (
"fmt"
)
// Define some basic arithmetic operations as first-class functions.
// These functions can be assigned to variables, passed as arguments, and returned from other functions.
var sum = func(a, b int) int { return a + b }
var minus = func(a, b int) int { return a - b }
@Tanmay451
Tanmay451 / closure_function.go
Last active May 21, 2024 14:08
Closures are functions that reference variables from outside their body. These variables are captured and remembered even after the outer function has finished executing.
package main
import (
"fmt"
)
// testFunction returns a closure. A closure is a function that captures variables from its surrounding scope.
// In this case, 'y' is captured and maintained between function calls.
func testFunction() func(int) int {
var y int // 'y' is initialized in the enclosing function's scope.