【发布时间】:2018-08-04 13:53:33
【问题描述】:
我是 Go 的初学者。 我尝试在我的本地计算机上构建一个静态 Web 服务器。 其实我已经看过How do you serve a static html file using a go web server?
我的问题是,如果我有 Home.html。
当我链接localhost:7777 时,我想打开Home.html。
类似于index.html,但我想将index.html 替换为Home.html。
这是我的代码:
package main
import (
"fmt"
"net/http"
"log"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world!")
}
func main() {
http.HandleFunc("/", helloHandler)
//
err := http.ListenAndServe(":7777", nil)
if err != nil {
log.Fatal("ListenAndServe", err)
} else {
log.Println("listen 7777")
}
}
如何重写这段代码?
这个问题的关键词是什么?
【问题讨论】:
-
引用的代码不提供任何静态文件,因此“我如何修改此代码以做完全不同的事情”是一个过于宽泛的问题。你能把它缩小到你遇到的一些特定问题吗?