Echo

From G1Joshi/Agent-Skills · View interactive version

Echo is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Echo Go web framework assistance covering routing, middleware, data binding, and JSON validation. Use when developing high-performance, lightweight microservices in Go.

How to use this skill

Skill file

Echo

Echo is a minimalist Go framework known for performance. v4 is stable and widely used for microservices.

When to Use

Quick Start

package main

import (
	"net/http"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

type User struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

func main() {
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	e.GET("/api/health", func(c echo.Context) error {
		return c.JSON(http.StatusOK, map[string]string{"status": "ok"})
	})

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

Core Concepts

Handler Registration & Parameter Extraction

Clean route grouping with context parameter extraction:

package main

import (
	"net/http"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

type UserResponse struct {
	ID       string `json:"id"`
	Username string `json:"username"`
}

func getUserHandler(c echo.Context) error {
	id := c.Param("id")
	queryFilter := c.QueryParam("include_details")

	return c.JSON(http.StatusOK, UserResponse{
		ID:       id,
		Username: "user_" + id + "_filter_" + queryFilter,
	})
}

func main() {
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	api := e.Group("/api/v1")
	api.GET("/users/:id", getUserHandler)

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

Request Payload Binding & Validation

Validating incoming JSON payloads with go-playground validator:

package main

import (
	"net/http"
	"github.com/go-playground/validator/v10"
	"github.com/labstack/echo/v4"
)

type CustomValidator struct {
	validator *validator.Validate
}

func (cv *CustomValidator) Validate(i interface{}) error {
	return cv.validator.Struct(i)
}

type CreateUserRequest struct {
	Email    string `json:"email" validate:"required,email"`
	Age      int    `json:"age" validate:"gte=18,lte=120"`
}

func createUser(c echo.Context) error {
	req := new(CreateUserRequest)
	if err := c.Bind(req); err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if err := c.Validate(req); err != nil {
		return echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
	}
	return c.JSON(http.StatusCreated, req)
}

Centralized Custom HTTP Error Handler

Uniform error responses conforming to RFC 7807:

func customHTTPErrorHandler(err error, c echo.Context) {
	code := http.StatusInternalServerError
	msg := "Internal Server Error"

	if he, ok := err.(*echo.HTTPError); ok {
		code = he.Code
		msg = he.Message.(string)
	}

	if !c.Response().Committed {
		_ = c.JSON(code, map[string]interface{}{
			"error":  true,
			"code":   code,
			"detail": msg,
		})
	}
}

Common Patterns

Custom Context and JWT Authentication Middleware

Problem: Extracting authenticated user identity into request context safely across handlers.

Solution:

Create custom middleware and access via c.Get:

func AuthMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		token := c.Request().Header.Get("Authorization")
		if token != "Bearer valid-token" {
			return echo.NewHTTPError(http.StatusUnauthorized, "Invalid token")
		}
		c.Set("userId", 42)
		return next(c)
	}
}

// In handler: userId := c.Get("userId").(int)

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------------------- | :------------------------------------------------------------- | :------------------------------------------------------------------------- |

| code=400, message=Syntax error: unexpected end of JSON input | Empty request body passed to c.Bind(&struct). | Verify request body contains valid JSON before binding. |

| http: superfluous response.WriteHeader call | Calling c.JSON() or c.String() twice in same handler flow. | Return immediately upon rendering response: return c.JSON(...). |

| routing conflict: ... has conflicting route with ... | Overlapping route definitions with wildcard/param matchers. | Adjust route paths or separate route groups to remove parameter ambiguity. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.