🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

github.com/Neil-uli/go-play/pattern-routing

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/Neil-uli/go-play/pattern-routing

v0.0.0-20210831045400-a9e03ae7909c
Source
Go
Version published
Created
Source

Pattern Routing

In Go is handled by the DefaultServeMux method which is an instance of ServeMux. When nil is passed to the handler parameter for the ListenAndServe function then the DefaultServeMux just indirectly calling http.DefaultServeMux.HandleFunc(...).

ServeMux allows variable parts in url patterns and stub out the routes we'd like the application to have.

Choosing a Muxer

Many web apps handle requests to URLs with variable parts such as /settings/godric or /notes/42. ServeMux handles HTTP requests by multiplexing over handlers registered to fixed patterns only, but remember that ServeMux is just one implementation of the Handler interface. net/http wisely defines the foundation Server and Requst, but leaves handling to anything that implements ServeHTTP(ResponseWriter, *Request).

  • Patterns with parameters and easy retrieval of their values
  • HTTP method matching rules
  • Route reversal to build redirection URLs from a handler

Example

HTTP VerbPathActionDescription
GET/noteslistlist multiple notes
POST/notescreatecreate a note
GET/notes/{id}readget a note
PUT/notes/{id}updateupdate a note
DELETE/notes/{id}deletedelete a note
$ curl -X POST http://localhost:8080/api/v1/notes      # note create
$ curl -X PUT http://localhost:8080/api/v1/notes/3     # update note 3
$ curl -X DELETE http://localhost:8080/api/v1/notes/3  # delete note 3

FAQs

Package last updated on 31 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts