Go語言服務器開發(fā)實現(xiàn)最簡單HTTP的GET與POST接口
更新時間:2015年02月08日 14:58:46 作者:books1958
這篇文章主要介紹了Go語言服務器開發(fā)實現(xiàn)最簡單HTTP的GET與POST接口,實例分析了Go語言http包的使用技巧,需要的朋友可以參考下
本文實例講述了Go語言服務器開發(fā)實現(xiàn)最簡單HTTP的GET與POST接口。分享給大家供大家參考。具體分析如下:
Go語言提供了http包,可以很輕松的開發(fā)http接口。以下為示例代碼:
復制代碼 代碼如下:
package webserver
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
func WebServerBase() {
fmt.Println("This is webserver base!")
//第一個參數(shù)為客戶端發(fā)起http請求時的接口名,第二個參數(shù)是一個func,負責處理這個請求。
http.HandleFunc("/login", loginTask)
//服務器要監(jiān)聽的主機地址和端口號
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方式傳遞的參數(shù)
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數(shù)據(jù)
bytes, _ := json.Marshal(result)
fmt.Fprint(w, string(bytes))
}
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
func WebServerBase() {
fmt.Println("This is webserver base!")
//第一個參數(shù)為客戶端發(fā)起http請求時的接口名,第二個參數(shù)是一個func,負責處理這個請求。
http.HandleFunc("/login", loginTask)
//服務器要監(jiān)聽的主機地址和端口號
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方式傳遞的參數(shù)
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數(shù)據(jù)
bytes, _ := json.Marshal(result)
fmt.Fprint(w, string(bytes))
}
NewBaseJsonBean用于創(chuàng)建一個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語言程序設計有所幫助。
相關文章
go實現(xiàn)文件的創(chuàng)建、刪除與讀取示例代碼
這篇文章主要給大家介紹了關于go如何實現(xiàn)文件的創(chuàng)建、刪除與讀取的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2019-02-02Golang RSA生成密鑰、加密、解密、簽名與驗簽的實現(xiàn)
RSA 是最常用的非對稱加密算法,本文主要介紹了Golang RSA生成密鑰、加密、解密、簽名與驗簽的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-11-11