GO使用http.HandleFunc()方法搭建一個網站程序,具體方法如下:
1.使用http.HandleFunc()方法獲取參數,并保存為 hello.go文件;
package main
import (
"io"
"log"
"net/http"
)
func helloHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world!")
}
func main() {
http.HandleFunc("/hello", helloHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err.Error())
}
}
2.編譯 hello.go文件;
$ go run hello.go
3.最后,在瀏覽器中訪問http://localhost:8080/hello即可;