This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://gist.github.com/hongster/04660a20f2498fb7b680 | |
// https://github.com/arpitbbhayani/tcpserver/blob/master/main.go | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net" | |
"time" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"time" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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. |