思路:
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)
}
}