Could not compile when using custom context


#1

Hello all,
This question looks similar to
https://forum.labstack.com/t/i-got-an-error-when-using-a-custom-context?source_topic_id=450
but it is not.

I am new to Go and Echo.
I am trying to do the recipe https://echo.labstack.com/guide/context
Here is my code and error:

github.com/kberov/slovo.go/slovo/controllers/home.go

package home

import (
	. "github.com/kberov/slovo.go/slovo/controllers"
	"github.com/labstack/echo"
	"net/http"
)

func Show(c *echo.Context) error {
	cc := c.(*SlovoContext)
	return cc.String(http.StatusOK, "OK")
}

ant in context.go I have

package controllers

import (
	"github.com/labstack/echo"
)

type (
	SlovoContext struct {
		echo.Context
	}
)

In my main.go

//Middleware function to register our context which extends the "echo" one.
func registerContext(h echo.HandlerFunc) echo.HandlerFunc {
	return func(c echo.Context) error {
		cc := &SlovoContext{c}

		return h(cc)
	}
}

//...
	// Middleware
	e.Use(registerContext)

The error is:

github.com/kberov/slovo.go/slovo/controllers/home

slovo/controllers/home/home.go:10: invalid type assertion: c.(*controllers.SlovoContext) (non-interface type *echo.Context on left)


#2

Hi again,
I found my mistake. The signature of the function was wrong - func Show(c *echo.Context) error.
It had to be func Show(c echo.Context) error.
My function had to accept echo.Context and not a pointer to it, if I understand correctly.
Thanks!