Go HTTP 服务器最小示例
minimal_go_http_server.txt
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
})
// log.Fatal 显示类似以下错误
// 端口已被使用
log.Fatal(http.ListenAndServe(":8080", nil))
}将此文件保存为 Main.go,放在名为 GoHTTPServer 的目录中,运行 go build && ./GoHTTPServer。然后,在浏览器中打开 http://localhost:8080 自行查看结果。
Check out similar posts by category:
Allgemein
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow