dongxiongshi9952 2015-05-04 18:39
浏览 62
已采纳

将元素添加到handlerfunc中的slice并整体返回

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:

  1. I know how to add a record to database
  2. 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 the rest.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.

  • 写回答

1条回答 默认 最新

  • dongliuliu0385 2015-05-04 22:00
    关注

    From what I can tell the problem is that you're writing to your db but you're reading from the cached version so the response of rss just doesn't reflect the model at the time the request is made. If you look at this code;

    database := model.ReadFileIntoSlice()
    
    // initialise mux router
    r := mux.NewRouter()
    
    // http handles
    r.HandleFunc("/add/{base64url}", rest.AddArticle(database))
    

    you somehow need to modify the value of database. There are many ways you could do this. A few options would be 1) define it on some object or at a package level and then directly modify it from wherever AddArticle is defined. 2) refresh your in memory version, ie before you return the results, read from the db again so you're assured to have the latest (obv performance implications) 3) don't pass database by value, make the argument a pointer instead. AddArticle is getting a copy of database rather than the address to the version you're reading from in your rss call. You could instead pass a pointer into that method so that the original copy is modified (it also performs substantially better as your model gets larger).

    Based on the simplicity of your program I'd probably do 3. Realistically 2 is a more robust solution and serious enterprise software probably would require something more along those lines (your model doesn't work if your app is load balanced or something like that).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题