How to send data echo context and some data to another package

Viewed 38

i have package main with echo framework example :

func main() {

var data map[string]interface{}

echo.NotFoundHandler = func(c echo.Context) error {
    var dataResponse st.DataResponse

    dataResponse = st.Response{
        Response: st.ResponseNil{
            ErrorCode:    "ER-900",
            ResponseCode: "099",
            ResponseDesc: "Method Not Found",
        },
    }

    return c.JSONPretty(http.StatusNotFound, dataResponse, "  ")
}

e := echo.New()
e.POST("/TestPost", control.Test)

fmt.Println("starting API")
e.Start(":1234")

}

how to send "data" to function Test() in package control? function test just not receive c context, but receive echo context and data like Test(echo.Context, data)

1 Answers

I think you want to take everything from c echo.Context in the http handler and place it in your own structs. Then when calling another package you can use a pointer to your struct and leave all the echo stuff in the main package or where you have all your http echo stuff. Above you don't seem to be filling up var data map[string]interface{} with anything. But that could work too. Just pass that to your next package or make it more formal as a struct with all sorts of fields.

Related