Sorry if this question is a bit basic however I have not been able to find any documentation on it. I am trying to import the following method from example.com/User/project/controllers
package
func (env *Env) Index(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params){
fmt.Fprintf(ctx, "Hi there! RequestURI is %q", ctx.RequestURI())
}
Into the following file to be used in a router as follows
db, err := db.Conn()
if err != nil {
log.Panic(err)
}
env := &Env{db}
...
router.GET("/", env.controllers.Index)///this import is not valid
I have tried to use controllers.env.Index
env.controllers.Index
I have also tried importing with a .
before the import .etc How would one in this instance import a method from another package whereby a struct (ENV
) can be passed to it? To clarify the problem here is using method on top of the package ontop of the helper e.g. method.package.helper
how would I resolve the above code so that I can pass a method to a helper from an external package Thanks
Just to clarify, you would like to pass in your db struct to your Env struct correct?
This may help if I am understanding correctly.
Since a struct is really just a collection of fields, it's dependent on the makeup of the Env struct.
What is the error that you are receiving?