Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 转到:提供静态页面_Go - Fatal编程技术网

Go 转到:提供静态页面

Go 转到:提供静态页面,go,Go,我试图用GO显示一个静态页面 去: 目录: Project /static home.html edit.html project.go 当我运行它时,网页显示edit.html和home.html的链接,而不是显示home.html中的静态页面。我做错了什么。这是提供文件的最佳方式吗?我知道还有其他方法,比如html/模板包,但我不确定它们的区别是什么,以及何时使用这些方法。谢谢 func main() { http.Handle("/

我试图用GO显示一个静态页面

去:

目录:

Project
    /static
        home.html
        edit.html
    project.go
当我运行它时,网页显示edit.html和home.html的链接,而不是显示home.html中的静态页面。我做错了什么。这是提供文件的最佳方式吗?我知道还有其他方法,比如html/模板包,但我不确定它们的区别是什么,以及何时使用这些方法。谢谢

func main() {
    http.Handle("/", http.FileServer(http.Dir("static")))
    http.ListenAndServe(":4747", nil)
}
您不需要
static/home
,只需要
static

正在使用,由于您在
/static
中没有
index.html
,因此将显示目录内容

一个快速修复方法是将
home.html
重命名为
index.html
。这将允许您通过
http://localhost:4747/
edit.html
http://localhost:4747/edit.html

如果只需要提供静态文件,则无需使用
html/template


但是,一个干净的解决方案取决于您实际要做什么。

如果您只对编写一个提供静态内容的简单服务器感兴趣,而不仅仅是作为一种学习体验,那么我想看看Martini()

为名为“public”的文件夹中的静态文件提供服务的经典Martini应用程序是:

package main

import (
    "github.com/go-martini/martini"
)

func main() {
    m := martini.Classic()
    m.Run()
}
将名为“static”的新静态文件夹添加到搜索内容的静态文件夹列表也很简单:

package main

import (
    "github.com/go-martini/martini"
)

func main() {
    m := martini.Classic()
    m.Use(martini.Static("static")) // serve from the "static" directory as well
    m.Run()
}
Martini还提供了更多的功能,如会话、模板呈现、路由处理程序等

我们在这里的生产中使用马提尼酒,对它及其周围的基础设施非常满意。

会有帮助吗?
package main

import (
    "github.com/go-martini/martini"
)

func main() {
    m := martini.Classic()
    m.Use(martini.Static("static")) // serve from the "static" directory as well
    m.Run()
}