An HTTP method is a standardized operation token in a request. Together with the path, it lets a router select the handler and expected response for a resource. section 8.1.1
Use methods to assign different operations to the same endpoint:
- GET commonly retrieves data, while POST commonly creates it. section 8.1.1
- PUT, PATCH, DELETE, and OPTIONS are available for APIs that require those operations, but client support may vary. section 8.1.1
- Avoid treating CRUD as a mandatory architecture; choose the endpoint design according to application goals. section 8.1.1
One comments endpoint, two handlers
Go's standard router can register method-specific patterns such as GET /comments and POST /comments. A GET handler can format stored comments, while a POST handler reads the request body, appends a comment, and records its timestamp. Test creation with curl -X POST http://localhost:8004/comments -d "I'm new here". section 8.1.1
Listing 8.1 Using built-in method routing
package main
import (
"net/http"
)
func getComments(w http.ResponseWriter,
r *http.Request) { #1
w.Write([]byte("here you'll get the comments"))
}
func postComments(w http.ResponseWriter,
r *http.Request) { #2
w.Write([]byte("thank you for posting a comment"))
}
func main() {
http.HandleFunc("GET /comments", getComments) #3
http.HandleFunc("POST /comments", postComments) #4
if err := http.ListenAndServe(":8000", nil); err != nil {
panic(err)
}
}
Shows a complete runnable Go server using built-in method routing for separate GET and POST handlers. section 8.1.1
When routing needs extra request-based decisions, a wrapper can inspect r.Method manually and dispatch further, such as separating comment creation from updating based on decoded JSON. section 8.1.1









