What is the ideal way to handle api versions in golang?

I am creating a server in the Golang intended for a mobile application. I need to be able to support multiple versions of the API for cases where users do not update the application. The main problem with the version is to return the data in the correct format for the version of the mobile application.

I saw that there are three main ways to do this.
A. One way is to have one route handler on "/" and then allow this function to parse the version control URL.
Example:

func main() { http.HandleFunc("/", routes.ParseFullURI) } 

C. Use a library such as gorilla / mux to process patterns inside the router, but I have seen some warnings that this might be too slow .
Example:

  func main() { mux.HandleFunc("{version:}/", routes.ParseVersionForHome) mux.HandleFunc("{version:}/getData", routes.ParseVersionForGetDAta) mux.HandleFunc("{version:}/otherCall", routes.ParseVersionForOtherCall) } 

C. Have separate URLs that do not change, but are split into different versions based on the header. Example:

 func main() { http.HandleFunc("/", routes.ParseHeaderForVersionForHome) http.HandleFunc("/getData", routes.ParseHeaderForVersionForGetData) http.HandleFunc("/otherCall", routes.ParseHeaderForVersionForOtherCall) } 

I am concerned that option 1 will be too dirty code. I am worried that option 2 will be too slow, and I am worried that for the third client it will be difficult to complete task 3 or it will be confusing as the version is not clearly marked.

Which method is the most idiomatic for Golang and will lead to maximum performance for the mobile application that will be frequently polled?

+5
source share
1 answer

There are many routing infrastructures that allow grouping, for example, using an echo (very good structure if you want speed)

 package main import "github.com/labstack/echo" func ping(c *echo.Context) { c.String(200, "pong") } func main() { e := echo.New() v1 := e.Group("/v1") v1.Get("/ping", ping) v2 := e.Group("/v2") v2.Get("/ping", ping) e.Run(":4444") } 

I think this is pretty clean.

I am sure that many other frameworks allow this. I know that it’s a martini, but it’s not an idiomatic structure ...

+5
source

All Articles