I'm writing a service to learn Go. My main function can be found below. It starts with reading an XML file and storing them in a slice. I have a /rss
endpoint which outputs a RSS feed from the items stored in the "database". This is working fine. I also have an endpoint (/add/{base64}
) which is used to add a new item to that slice. Unfortunately I don't know how to do this. For some reason I need to return the new database
with the added record, so it gets available to the /rss
. But how?
My concrete problem is:
- I know how to add a record to
database
-
But I don't know how to return the full (including the added) database so the
/rss
endpoint is able to use it. So I want to let therest.AddArticle
return the new database so the/rss
endpoint knows the added item.func main() { defer glog.Flush() // read database database := model.ReadFileIntoSlice() // initialise mux router r := mux.NewRouter() // http handles r.HandleFunc("/add/{base64url}", rest.AddArticle(database)) r.HandleFunc("/rss", rest.GenerateRSS(database)) // start server http.Handle("/", r) glog.Infof("running on port %d", *port) http.ListenAndServe(":"+strconv.Itoa(*port), nil) }
Or is there some other solution which does the job? I just want database
to be available through all packages.