net/http 01

发布时间 2023-11-14 21:51:50作者: 201432273

用 net/http 包可以快速起一个 web 服务

点击查看代码
package main

import (
    "fmt"
    "net/http"
)

func helloWorld(w http.ResponseWriter, req *http.Request) {
    _, err := fmt.Fprintf(w, "Hello world!")
    if err != nil {
        fmt.Println("code: 500")
    }
}

func main() {
    http.HandleFunc("/hello",helloWorld)
    err := http.ListenAndServe("0.0.0.0:9000", nil)
    if err != nil {
        fmt.Println("Listen is failed", err)
    }
}