Ok i have a main package and a http handler package. Essentially what i am trying to do is setup a global struct so that way i can call upon information in that struct at any time.
Basic outline of my attempted example below: Main package imports handler function Main package calls handlerfunc Handlerfunc sets http.ResponseWriter and other items into UrlInfo struct Handlerfunc runs passed in function (without having to pass UrlStruct into function) Run function (home in this example) Function home can call upon variable uinfo at any time cause its a pointer UrlInfo struct
Obviously this doesnt work, but this is essentially what i would like to do so that way im not having to pass all this info into my home function. Keeping it clean and simple.
Any thoughts and ideas are welcome. Thanks.
Handler Package
package handler
// Struct containing http requests and variables
type UrlInfo struct {
Res http.ResponseWriter
Req *http.Request
Item string
}
func HandleFunc(handlepath string, runfunc func()) {
// Set handler and setup struct
http.HandleFunc(handlepath, func(w http.ResponseWriter, r *http.Request) {
url := new(UrlInfo)
url.Res = w
url.Req = r
url.Item = "Item information"
runfunc()
})
}
Main Package
import "handler"
var uinfo = &handler.UrlInfo{}
func init() {
handler.HandleFunc("/home/", home)
}
func home() {
fmt.Println(uinfo.Item)
}