ithzhang 2019-12-21
package main import ( "github.com/go-kit/kit/transport/http" "gomicro/Services" ) func main() { user := Services.UserService{} endp := Services.GenUserEnPoint(user) http.NewServer(endp, Services.DecodeUserRequest, Services.EncodeUserResponse) //使用go kit创建server传入我们之前定义的两个解析函数 } /* func DecodeUserRequest(c context.Context, r *http.Request) (interface{}, error) { //这个函数决定了使用哪个request来请求 if r.URL.Query().Get("uid") != "" { //request会先进入这个函数去解析决定使用哪个request结构体来请求 uid, _ := strconv.Atoi(r.URL.Query().Get("uid")) return UserRequest{Uid: uid}, nil } return nil,errors.New("参数错误") } func EncodeUserResponse(ctx context.Context,w http.ResponseWriter,response interface{}) error{ w.Header().Set("Content-type","application/json") //设置响应格式为json,这样客户端接收到的值就是json,就是把我们设置的UserResponse给json化了 return json.NewEncoder(w).Encode(response) } */