golang实现http服务器处理静态文件示例

2019-11-10 10:35:49王旭

        if req.URL.Path[len(req.URL.Path)-1] != '/' {
            http.Redirect(w, req, req.URL.Path+"/", 301)
            return
        }
        for _, index := range indexs {
            fi, err = os.Stat(file + index)
            if err != nil {
                continue
            }
            http.ServeFile(w, req, file+index)
            return
        }
        http.NotFound(w, req)
        return
    }
    http.ServeFile(w, req, file)
}

希望本文所述对大家Go语言程序设计有所帮助。