Skip to content

Instantly share code, notes, and snippets.

@surfmuggle
Last active August 6, 2024 09:13
Show Gist options
  • Save surfmuggle/af30ff908f8e20e9d270252e303191df to your computer and use it in GitHub Desktop.
Save surfmuggle/af30ff908f8e20e9d270252e303191df to your computer and use it in GitHub Desktop.

Dependencies or standard

With the release of Go 1.22 the standard library offers some features that reduce the need / the benefit of some dependencies.

The patterns used by net/http.ServeMux now accept methods and wildcards.

For example, the router accepts a pattern like GET /task/{id}/, which matches only GET requests and captures the value of the {id} segment in a map that can be accessed through Request values.

Based on How to create your Golang APIs from now on with v1.22 i wonder what someone new to Go should do.

Compare templ with html/template

Question / Prompt to ChatGPT

What does https://github.com/a-h/templ offer that html/template from the standard library does not offer?

Response

The templ package from the repository https://github.com/a-h/templ offers several features that go beyond the capabilities of the html/template package from the Go standard library. Here’s a comparison of what templ offers compared to html/template:

Templ vs. html/template

  1. Type Safety:

    • templ: Provides type-safe templates, ensuring that templates are checked at compile-time for correctness. This helps catch errors early in the development process.
    • html/template: Is not type-safe and errors can only be caught at runtime.
  2. Go Code Integration:

    • templ: Allows embedding Go code directly within the templates, offering a more integrated experience and reducing the need to switch between different languages or contexts.
    • html/template: Requires separate logic for templates and template execution, often leading to more boilerplate code.
  3. Better Developer Experience:

    • templ: Supports features like Go to definition, find references, auto-completion, and renaming within templates when using supported editors like Visual Studio Code.
    • html/template: Does not offer such integrated development features, making template development more cumbersome.
  4. Modern Syntax and Features:

    • templ: Uses a more modern and expressive syntax, supporting features like conditionals, loops, and components in a more concise manner.
    • html/template: Has a more traditional and verbose syntax, which can be less intuitive for complex templates.
  5. Built-in HTML Escaping:

    • Both templ and html/template offer HTML escaping to protect against cross-site scripting (XSS) attacks. However, templ enforces stricter type checking which can help prevent logical errors that might lead to unescaped content.
  6. Performance:

    • templ: Generates Go code from templates, which is then compiled. This can lead to better performance as the template rendering is essentially compiled Go code.
    • html/template: Parses and executes templates at runtime, which can be less efficient.

Example Features of Templ

  • Components:

    // Define a component
    component Button(label string) string {
      return fmt.Sprintf("<button>%s</button>", label)
    }
    
    // Use the component
    fmt.Println(Button("Click me"))
  • Loops and Conditionals:

    // Loop example
    {{ range items }}
      <div>{{ .Name }}</div>
    {{ end }}
    
    // Conditional example
    {{ if .IsAdmin }}
      <div>Welcome, admin!</div>
    {{ else }}
      <div>Welcome, user!</div>
    {{ end }}

Summary

The templ package provides a more robust and developer-friendly approach to templating in Go, focusing on type safety, better integration with Go code, and improved developer experience through modern IDE features. This makes it particularly appealing for developers looking for a more integrated and less error-prone templating solution compared to the standard html/template package.

Compare gorilla/mux with standard library net

Question / Prompt to ChatGPT

What does gorilla/mux offer over the standard library

Answer

The gorilla/mux package offers several features and enhancements over the standard library’s net/http package for routing and handling HTTP requests in Go. Here are some of the key advantages and features that gorilla/mux provides:

1. Named Routes:

  • gorilla/mux allows you to name your routes, making it easier to generate URLs and link to other parts of your application programmatically.
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler).Name("productRoute")
url, err := r.Get("productRoute").URL("key", "123")

2. Path Variables and Regular Expressions:

  • gorilla/mux supports extracting variables from the URL path and can also match routes using regular expressions.
r := mux.NewRouter()
r.HandleFunc("/products/{category}/{id:[0-9]+}", ProductHandler)

3. Subrouters:

  • Subrouters allow you to create nested routers, which is useful for grouping routes that share common properties or middleware.
r := mux.NewRouter()
s := r.PathPrefix("/products").Subrouter()
s.HandleFunc("/{category}/{id:[0-9]+}", ProductHandler)

4. Middleware:

  • gorilla/mux makes it easy to define middleware for your routes, which can be used to handle cross-cutting concerns like logging, authentication, and more.
r := mux.NewRouter()
r.Use(loggingMiddleware)

5. HTTP Methods and Headers:

  • You can restrict routes to specific HTTP methods and match based on request headers.
r := mux.NewRouter()
r.HandleFunc("/products", ProductsHandler).Methods("GET")
r.HandleFunc("/products", CreateProductHandler).Methods("POST")
r.HandleFunc("/products/{id}", DeleteProductHandler).Methods("DELETE").Headers("X-Custom-Header", "value")

6. Hostnames and Schemes:

  • gorilla/mux can match routes based on hostnames and URL schemes, allowing for more flexible routing configurations.
r := mux.NewRouter()
r.HandleFunc("/secure", SecureHandler).Schemes("https")
r.HandleFunc("/insecure", InsecureHandler).Schemes("http")
r.HandleFunc("/", HomeHandler).Host("www.example.com")

7. Path Prefix Matching:

  • It supports matching based on path prefixes, which is useful for APIs and grouping routes.
r := mux.NewRouter()
api := r.PathPrefix("/api").Subrouter()
api.HandleFunc("/users", UsersHandler)
api.HandleFunc("/products", ProductsHandler)

8. Route Matching and Dispatching:

  • gorilla/mux provides more advanced matching options and better dispatching mechanisms compared to the standard library.

Example Comparison

Standard Library (net/http)

http.HandleFunc("/products", ProductsHandler)
http.HandleFunc("/products/", ProductDetailsHandler)
log.Fatal(http.ListenAndServe(":8080", nil))

gorilla/mux

r := mux.NewRouter()
r.HandleFunc("/products", ProductsHandler)
r.HandleFunc("/products/{id:[0-9]+}", ProductDetailsHandler)
log.Fatal(http.ListenAndServe(":8080", r))

Summary

gorilla/mux provides a more powerful and flexible routing solution than the standard library’s net/http package. It includes features such as named routes, path variables, subrouters, middleware support, HTTP method and header matching, hostname and scheme matching, and path prefix matching. These features make it easier to build complex and maintainable web applications in Go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment