社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
首先,你需要有net/http库,这个我是没有专门去安装,应该是安装golang的时候就带上了,在{GOPATH}src目录下。
这里讲一种简单的实现。
package main
import (
"fmt"
"net/http"
)
//HTTPHandlerHello 无参数
func HTTPHandlerHello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world, n URL %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/test/usr/ddddd", HTTPHandlerHello)
http.ListenAndServe(":8080", nil)
}
很简单的处理,就是监听了localhost的8080端口,然后向页面输出hello world, URL。
首先在 vscode上编码,基础的环境配置可以参考我之前写的vscode下go搭建。https://blog.csdn.net/ZHAOJUNWEI08/article/details/83576765
直接go build server.go,编译
然后去双击运行server.exe
之后在浏览器上输入:http://127.0.0.1:8080/test/usr/abcd或者http://localhost:8080/test/usr/abcd,Enter键,如下:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!