douyueqing1530 2012-10-05 01:03
浏览 182
已采纳

为什么filepath.Walk()恐慌?

I am trying to code up the wiki for go programing language. I have put the data in /data folder and .html files in the /tmpl folder.

I have coded up the main function as follows

func main() {
    walk := func(path string, info os.FileInfo, err error) error {
        if err == nil && !info.IsDir() && filepath.Ext(path) == ".html" {
            http.HandleFunc("/view/", makeHandler(viewHandler))
            http.HandleFunc("/edit/", makeHandler(editHandler))
            http.HandleFunc("/save/", makeHandler(saveHandler))
        }
        return err
    }
    root := TEMPLATE_PATH + "edit.html"
    err := filepath.Walk(root, walk)
    if err != nil {
        return
    }
}

I am getting panic edit.html not found. Could anyone please guide me?

  • 写回答

1条回答 默认 最新

  • dtnpzghys01643322 2012-10-05 01:45
    关注

    From the documentation of filepath.Walk you can read:

    func Walk(root string, walkFn WalkFunc) error

    Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient.

    So Walk expects a directory as first parameter. You're putting a file in there, which makes the whole thing throw a panic.

    I'm not sure what you're attempting to do, but if you want to open that file, use os.Open or os.Stat if you want to test for existance.

    A general tip when coding in go: Use the gofmt command to format your code according to the style guidelines.

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

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题