思路:
1、导包
2、写hander(Index,Message,Welcome)及触发后网页中的html的内容
3、利用绑定hander给http
4、开启监听端口
5、运行触发
package main
import (
"fmt"
"net/http"
)
func Index(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hello world, this is my first page!")
w.Header().Set("Content-Type", "text/html")
html := `<doctype html> <html> <head> <title>Page Index</title> </head> <body> <p> <a href="/Welcome">Welcome</a> | <a href="/Message">Message</a> </p> </body> </html>`
fmt.Fprintln(w, html)
}
func Message(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hello world, this is my first page!")
w.Header().Set("Content-Type", "text/html")
html := `<doctype html> <html> <head> <title>Page Message</title> </head> <body> <p> <a href="/Index">Index</a> | <a href="/Welcome">Welcome</a> </p> </body> </html>`
fmt.Fprintln(w, html)
}
func Welcome(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hello world, this is my first page!")
w.Header().Set("Content-Type", "text/html")
html := `<doctype html> <html> <head> <title>Page Welcome</title> </head> <body> <p> <a href="/Index">Index</a> | <a href="/Message">Message</a> </p> </body> </html>`
fmt.Fprintln(w, html)
}
func main() {
http.HandleFunc("/Index", Index)
http.HandleFunc("/Message", Message)
http.HandleFunc("/Welcome", Welcome)
// 监听本机的8030端口
err := http.ListenAndServe(":8030", nil)
if err != nil {
fmt.Println("Error: ", err)
}
}
本文介绍如何在Golang中创建基础网页并实现页面间的跳转。首先讲解了导入必要的包,然后详细阐述了编写Index、Message和Welcome三个处理器函数以及对应的HTML内容。接着说明了如何将处理器绑定到HTTP并启动监听端口。最后,通过运行程序来实际触发这些功能。
505

被折叠的 条评论
为什么被折叠?



