簡單版本:
package main
import (
"fmt"
"net/http"
)
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Hello !</h1>")
}
func main() {
http.HandleFunc("/", index)
http.ListenAndServe(":80", nil)
fmt.Println("伺服器已啟動...")
}
更換頁面版本:
package main
import (
"fmt"
"net/http"
)
func index(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Hello %s!</h1><br><br><a href=/sub>到 sub</a>", r.URL.Path[1:])
}
func sub(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Hello %s!</h1><br><br><a href=/>到 index</a>", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", index)
http.HandleFunc("/sub", sub)
http.ListenAndServe(":80", nil)
fmt.Println("伺服器已啟動...")
}
留言列表