Go语言,十分方便开发Api接口。而且不需要web服务器就能够实现。
现在想返回指定格式的Api,格式如下
1 2 3 4 5 6
| { "code": 400, "data": {}, "message": "非法访问!" }
|
code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package web
import ( "fmt" "net/http" "time" "encoding/json" )
func main() { fmt.Print("ready") http.HandleFunc("/login", loginTask)
err := http.ListenAndServe("localhost:8081", nil)
if err != nil{ fmt.Println("ListanAndServer error: ", err.Error()) }
}
func loginTask(w http.ResponseWriter, req *http.Request){ fmt.Println("loginTash is running...")
time.Sleep(time.Second * 2)
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 = "登录成功!" result.Date = struct {
}{} } else { result.Code = 101 result.Message = "密码错误" result.Date = struct { }{} }
bytes, _ := json.Marshal(result) fmt.Fprint(w, string(bytes)) }
type BaseJsonBean struct{ Code int `json:"code"` Date interface{} `json:"data"` Message string `json:"message"` }
func newBaseJsonBean() *BaseJsonBean{ return &BaseJsonBean{} }
|
测试
访问localhost:8081/login,使用post方式
传递参数
1 2
| username:zhangsan password:123456
|
返回信息
1 2 3 4 5
| { "code": 100, "data": {}, "message": "登录成功!" }
|