golang net包使用

创建简单的响应服务器

package main

import (
	"net/http"
)

func handlerFunc(w http.ResponseWriter, r *http.ReadRequest) {
	w.Write([]byte("hello world"))
}

func main() {
	http.HandleFunc("/hello", handlerFunc) // 第一个参数为路由,第二个参数为要注册的回调函数

	http.ListenAndServe(":8081", nil) // 绑定函数地址
}

1、注册回调函数 http.HandleFunc("/",“handlefunc”)

      p1: 路由

      p2: “/handlefunc” 回调函数名,需定义函数,要求函数必须为(w http.ResponseWriter, r *http.ReadRequest) 作为参数的

2、绑定服务器监听地址 http.ListenAndServe p2传入nil时,会自动调用系统自行封装的函数进行处理请求。

 

猜你喜欢

转载自www.cnblogs.com/LC161616/p/10739523.html