建立程序目录:
mkdir hello\src\hello
cd hello\src\hello
go mod init hello
在 hello\src\hello 目录下创建main.go,代码如下:
package main
import(
"github.com/gorilla/mux"
"net/http"
"log"
)
func main(){
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
http.Handle("/", router)
err := http.ListenAndServe(":8000", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
运行以上代码:

网友评论