Skip to content

Instantly share code, notes, and snippets.

@alexedwards
alexedwards / index.html
Last active September 17, 2021 09:56
HTML skeleton
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Your Page Title</title>
<meta name="description" content="Brief description">
<meta name="author" content="Your name">
func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) {
id, err := app.readIDParam(r)
if err != nil {
// IT'S THIS LINE HERE THAT YOU NEED TO CHANGE to use the new notFoundResponse() helper.
app.notFoundResponse(w, r)
return
}
movie := data.Movie{
ID: id,
CreatedAt: time.Now(),
package main
import (
"net/http"
"github.com/bmizerany/pat"
"github.com/justinas/alice"
)
func (app *application) routes() http.Handler {
package jsonlog
import (
"encoding/json"
"io"
"os"
"runtime/debug"
"sync"
"time"
)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Simple CORS</h1>
<output></output>
<script>
document.addEventListener('DOMContentLoaded', function() {
@alexedwards
alexedwards / main.go
Created November 15, 2020 10:38
JSON decoding benchmarks
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
func createMovieHandlerUnmarshal(w http.ResponseWriter, r *http.Request) {
var input struct {
@alexedwards
alexedwards / main.go
Created November 10, 2020 10:20
JSON encoding benchmarks #2
package main
import (
"encoding/json"
"net/http"
)
func main() {}
func healthcheckHandlerMarshalIndent(w http.ResponseWriter, r *http.Request) {
@alexedwards
alexedwards / main.go
Last active January 24, 2025 03:27
JSON encoding benchmarks
package main
import (
"encoding/json"
"net/http"
)
func main() {}
func healthcheckHandlerEncoder(w http.ResponseWriter, r *http.Request) {
@alexedwards
alexedwards / _tree
Created October 27, 2020 07:52
Dependency injection via closure (handlers in multiple packages)
.
├── go.mod
├── handlers
│ ├── books
│ │ └── books.go
│ └── env.go
├── main.go
└── models
└── models.go
@alexedwards
alexedwards / _tree
Created October 27, 2020 07:40
Config package with global variable
.
├── config
│ └── config.go
├── go.mod
├── go.sum
├── main.go
└── models
└── books
└── books.go