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
| import ( | |
| "strings" | |
| "testing" | |
| "golang.org/x/net/html" | |
| "github.com/andybalholm/cascadia" | |
| ) | |
| func containsHTMLNode(t *testing.T, htmlBody, cssSelector string) bool { | |
| doc, err := html.Parse(strings.NewReader(htmlBody)) | |
| if err != nil { |
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 | |
| // This example shows how to create a custom command-line flag by implementing | |
| // the flag.Value interface. The flag accepts a comma-separated list of values | |
| // and stores the contents in a DomainList type, which has the underlying type | |
| // []string. | |
| // | |
| // Use it like: | |
| // go run main.go -domains="example.com, example.org" |
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
| func main() { | |
| router := httprouter.New() | |
| router.HandlerFunc("GET", "/", indexGet) | |
| router.HandlerFunc("POST", "/", indexPost) | |
| err := http.ListenAndServe(":3000", router) | |
| log.Fatal(err) | |
| } |
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
| func TestRouter(t *testing.T) { | |
| used := "" | |
| mw1 := func(next http.Handler) http.Handler { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| used += "1" | |
| next.ServeHTTP(w, r) | |
| }) | |
| } |
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
| func TestChain(t *testing.T) { | |
| used := "" | |
| mw1 := func(next http.Handler) http.Handler { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| used += "1" | |
| next.ServeHTTP(w, r) | |
| }) | |
| } |
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
| mux := http.NewServeMux() | |
| mux.Handle("/", recoverMiddleware(requestIDMiddleware(loggingMiddleware(http.HandlerFunc(homeHandler))))) | |
| mux.Handle("/about", recoverMiddleware(requestIDMiddleware(loggingMiddleware(http.HandlerFunc(aboutHandler))))) | |
| mux.Handle("/admin", recoverMiddleware(requestIDMiddleware(loggingMiddleware(authenticationMiddleware(adminRoleCheckMiddleware(http.HandlerFunc(adminDashboardHandler))))))) |
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
| func (app *application) rateLimit(next http.Handler) http.Handler { | |
| if !app.config.limiter.enabled { | |
| return next | |
| } | |
| type client struct { | |
| limiter *rate.Limiter | |
| lastSeen time.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
| . | |
| ├── cmd | |
| │ ├── cli | |
| │ └── web | |
| ├── internal | |
| │ ├── database | |
| │ ├── request | |
| │ ├── response | |
| │ ├── templatefuncs | |
| │ ├── validator |
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 cache | |
| import ( | |
| "sync" | |
| "time" | |
| ) | |
| // Cache is a basic in-memory key-value cache implementation. | |
| type Cache[K comparable, V any] struct { | |
| items map[K]V // The map storing key-value pairs. |
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
| #!/bin/bash | |
| # Check if the version argument is provided | |
| if [ $# -ne 1 ]; then | |
| echo "Usage: $0 <go_version>" | |
| exit 1 | |
| fi | |
| # Version number provided as an argument | |
| version="$1" |
NewerOlder