Go语言提供了http包,可以很轻松的开发http接口。以下为示例代码:
package webserver import ( "encoding/json" "fmt" "net/http" "time" ) func WebServerBase() { fmt.Println("This is webserver base!") //第一个参数为客户端发起http请求时的接口名,第二个参数是一个func,负责处理这个请求。 http.HandleFunc("/login", loginTask) //服务器要监听的主机地址和端口号 err := http.ListenAndServe("192.168.1.27:8081", nil) if err != nil { fmt.Println("ListenAndServe error: ", err.Error()) } } func loginTask(w http.ResponseWriter, req *http.Request) { fmt.Println("loginTask is running...") //模拟延时 time.Sleep(time.Second * 2) //获取客户端通过GET/POST方式传递的参数 req.ParseForm() param_userName, found1 := req.Form["userName"] param_password, found2 := req.Form["password"] if !(found1 && found2) { fmt.Fprint(w, "请勿非法访问") return } result := NewBaseJsonBean() userName := param_userName[0] password := param_password[0] s := "userName:" + userName + ",password:" + password fmt.Println(s) if userName == "zhangsan" && password == "123456" { result.Code = 100 result.Message = "登录成功" } else { result.Code = 101 result.Message = "用户名或密码不正确" } //向客户端返回JSON数据 bytes, _ := json.Marshal(result) fmt.Fprint(w, string(bytes)) }
NewBaseJsonBean用于创建一个struct对象:
package webserver type BaseJsonBean struct { Code int `json:"code"` Data interface{} `json:"data"` Message string `json:"message"` } func NewBaseJsonBean() *BaseJsonBean { return &BaseJsonBean{} }
Go语言(服务器开发):实现最简单的HTTP GET/POST接口
原文地址:http://blog.csdn.net/books1958/article/details/41748719