当前位置:网站首页>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
}]
}最后
边栏推荐
- 第五届数字中国建设峰会在福建福州开幕
- pinia 入门及使用
- pycharm配置opencv库
- Ionic4 learning notes 8 -- UI component 2 list (no practice, direct excerpt)
- 2022 the latest short video de watermarking analysis API interface sharing
- Cookies and session "suggestions collection"
- Web penetration experience summary ing
- 移动端实现0.5px的实用方案
- ES6 cycle filter value
- jmeter --静默运行
猜你喜欢

6126. 设计食物评分系统

9. BOM object?

About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed

怎么解决idea中yaml无法识别或者飘红?

Missing value processing

5. Reference type and value type as function parameters?

Wechat applet

微信小程序逆向

根证书的有效期与服务器SSL证书一样长吗?

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery
随机推荐
【校验】只能输入数字(正负数)
Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它
关于接口的写法 1链式判读 ?. 2方法执行 (finally)一定会执行
BOM understanding in odoo
如何为超级通胀做好准备
Model saving and loading of sklearn
Flink operation Hudi data table
The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
6126. 设计食物评分系统
About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed
Handwritten blog platform ~ the next day
Space three point circle code
XSS绕过姿势总结
字符串常用方法(2)
Ionic4 learning notes 8 -- UI component 2 list (no practice, direct excerpt)
JMeter -- prometheus+grafana server performance visualization
Go language interface and type
【obs】视频、音频编码与rtmp发送的配合
The collapse of margin
Three ways of redis cluster