使用Go搭建一个WEB服务
sonder 中杯

使用Go搭建服务比较方便,引入net/http即可

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

package main

import (
"fmt"
"net/http"
"encoding/json"
)

// Data 定义一个数据结构
type Data struct{
Name string `json:"name"`
Age int `json:"age"`
Sex bool `json:"sex"`
}


func testApi(w http.ResponseWriter, r *Request){
// 初始一个json数据
data := Data{
Name:"sonderss",
Age:24,
Sex:true,
}
r.ParseForm() // 解析参数

jsonStu, _ := json.Marshal(data) // 将数据转为json对象格式

w.Write(jsonStu)
}


func main(){
// 定义url调用对应的业务接口
http.HandleFunc("/",testApi)
// 监听端口
http.ListenAndServe(":8005", nil)
}

  • 本文标题:使用Go搭建一个WEB服务
  • 本文作者:sonder
  • 创建时间:2020-07-10 22:17:30
  • 本文链接:https://sonderss.github.io/2020/07/10/使用Go搭建一个WEB服务/
 评论