dongqiao8421 2018-01-06 23:33
浏览 39

将mux.Walk的结果发送到ResponseWriter

This is the first time I am writing any golang code and am building a basic web api. What I want to do is whenever a request is received on "/" path, I want to write out all the paths of mux.Walk() to my ResponseWriter. Since the Walk function takes an anonymous function, how can I make the handler wait until all the paths are calculated and entered into my array which I can send out? If not waiting, is there a more efficient way of doing this?

  • 写回答

1条回答 默认 最新

  • dsq1982 2018-01-08 07:12
    关注

    Since the Walk function takes an anonymous function, how can I make the handler wait until all the paths are calculated and entered into my array which I can send out?

    The output of the Walk function does not change, since you'll eventually end up with a static binary. In other words, you only need to get the ouput of the Walk function once.

    If not waiting, is there a more efficient way of doing this?

    Have a look at the sample code provided in the official github repository.

    You can build your response for walk after the routes are first initialised, store this response locally, and return it once you receive a request for the available endpoints.

    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序