Versioning your REST API
When you create a RESTful API to serve an internal client, you probably don't have to worry about versioning your API. Taking things a step further, if you have control over all the clients that access your API, the same may be true.
However, in a case where you have a public API or an API where you do not have control over every client using it, versioning of your API may be required, as businesses need to evolve, which we will be covering in this recipe.
How to do it…
- Install the
github.com/gorilla/mux
package, using thego get
command, as follows:
$ go get github.com/gorilla/mux
- Create
http-rest-versioning.go
where we will define two versions of the same URL path that support the HTTPGET
method, with one havingv1
as a prefix and the other one withv2
as a prefix in the route, as follows:
package main import ( "encoding/json" "log" "net/http" "strings" "github.com/gorilla/mux" ) const ( CONN_HOST = "localhost" CONN_PORT = "8080" ) type Route struct...