golang 简单的web服务示例
package main import ( "log" "net/http" ) func SayHello(w http.ResponseWriter, req *http.Request) { w.Write([]byte("Hello world")) } func main() { http.HandleFunc("/", SayHello) err := http.ListenAndServe(":8080", nil) if err != nil { log.Println("服务器启动失败") log.Fatalln(err) } }
命令行,go run main.go,在浏览器访问localhost:8080