DEV Community

krishna
krishna

Posted on

3 2

API versioning in golang ECHO

package main

import (
    "net/http"

    "github.com/labstack/echo"
)

func main() {
    e := echo.New() 

    v1 := e.Group("/v1")
    groupV1Routes(v1)

    v2 := e.Group("/v2")
    groupv2Routes(v2)

    e.Logger.Fatal(e.Start(":8090"))
}

func groupV1Routes(e *echo.Group) {
    grA := e.Group("/groupa")
    grA.GET("/event", handlerGet1)

    grB := e.Group("/groupb")
    grB.PATCH("/:id", handlerPost)
}

func groupv2Routes(e *echo.Group) {
    grA := e.Group("/groupa")   
    grA.GET("/event", handlerGet2)

}

func handlerGet1(c echo.Context) error {
    return c.String(http.StatusOK, "THIS IS VERSION 1!")
}

func handlerGet2(c echo.Context) error {
    return c.String(http.StatusOK, "THIS IS VERSION TWO")
}

func handlerPost(c echo.Context) error {
    return c.String(http.StatusOK, c.Param("id"))
}

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay