本文实例讲述了Go语言服务器开发实现最简单HTTP的GET与POST接口。分享给大家供大家参考。具体分析如下:
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))
}
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{}
}
type BaseJsonBean struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Message string `json:"message"`
}
func NewBaseJsonBean() *BaseJsonBean {
return &BaseJsonBean{}
}
希望本文所述对大家的Go语言程序设计有所帮助。