当前位置:网站首页>Go小白实现一个简易的go mock server
Go小白实现一个简易的go mock server
2022-07-24 18:26:00 【InfoQ】
前言
目的
代码
步骤1
// 获取当前json文件所在的路径已经文件名,然后进行拼接
func ParsePath() (path string, err error) {
pwd, _ := os.Getwd()
fileList, err := ioutil.ReadDir(pwd)
if err != nil {
log.Fatal(err)
}
var curJsonFile string
for _, v := range fileList {
name := v.Name()
if isJson := strings.Contains(name, "json"); isJson {
curJsonFile = name
}
}
if curJsonFile == "" {
return curJsonFile, errors.New("dont have json file")
}
absPath := pwd + "/" + curJsonFile
return absPath, nil
}步骤2
// 根据传入的文件名读取具体的[]byte数据
func ReadJSON(path string) ([]byte, error) {
jsonFile, err := os.Open(path)
if err != nil {
log.Fatal("open file err")
}
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, err
}
return byteValue, nil
}步骤3
// 根据传入的[]byte数据,将数据转换反序列化为map类型
// 这里由于不知道json文件的具体数据,具体结构,所以用了map[string]interface{}的
// 数据类型
func DecodeJSONString(jsonBytes []byte) (map[string]interface{}, error) {
var m map[string]interface{}
err := json.Unmarshal(jsonBytes, &m)
if err != nil {
log.Fatal("json unmarshal fail", err)
return nil, err
}
return m, nil
}var data map[string]interface{}
func returnResponse(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
path := r.URL.Path
if d := data[path]; d != nil {
stringData, err := json.Marshal(data[path])
if err != nil {
log.Fatal(err)
}
w.Write(stringData)
}
}
func main() {
path, err := src.ParsePath()
if err != nil {
log.Fatal(err)
}
bytes, err := src.ReadJSON(path)
if err != nil {
log.Fatal(err)
}
m, err := src.DecodeJSONString(bytes)
data = m
if err != nil {
log.Fatal("decode fail: ", err)
}
for addr, _ := range data {
http.HandleFunc(addr, returnResponse)
}
err = http.ListenAndServe(":3000", nil)
if err != nil {
log.Fatal("listenAndServer fail")
}
}最终效果
localhost:3000/v1/lottery
{
"/v1/lottery": [{
"name": "shinji",
"age": 20
}, {
"name": "ayanami",
"age": 20
}]
}最后
边栏推荐
猜你喜欢

【刷题记录】20. 有效的括号

Cf. bits and pieces (subset pressing DP + pruning)

模拟实现vector

Mid year inventory | in 2022, PAAS will be upgraded again

JMeter -- silent operation

How to solve the problem that yaml in idea is unrecognized or red?

Laravel notes - RSA encryption of user login password (improve system security)

Install jumpserver

jmeter --静默运行

5. Reference type and value type as function parameters?
随机推荐
初识Pytorch和Pytorch环境配置
Cookies and session "suggestions collection"
JMeter -- prometheus+grafana server performance visualization
字符串常用方法(2)
数组扁平化.flat(Infinity)
odoo中的bom理解
Read zepto source code touch module
web渗透经验汇总ing
EasyUI adds row level buttons to the DataGrid
【OpenCV】—阈值化
Emerging potential of interactive virtual reality technology in drug discovery
猜JWT关键字
Missing value processing
Inheritance and Derive
排序的几种方式for while 还有sort
如何用WebGPU流畅渲染百万级2D物体?
Windowing function (1) - top three employees of department salary
Guess JWT keyword
Growth of operation and maintenance Xiaobai - week 8 of Architecture
数组常用方法(2)