用 net/http 包可以快速起一个 web 服务
| package main |
| |
| import ( |
| "fmt" |
| "net/http" |
| ) |
| |
| func helloWorld(w http.ResponseWriter, req *http.Request) { |
| _, err := fmt.Fprintf(w, "Hello world!") |
| if err != nil { |
| fmt.Println("code: 500") |
| } |
| } |
| |
| func main() { |
| http.HandleFunc("/hello", helloWorld) |
| err := http.ListenAndServe("0.0.0.0:8080", nil) |
| if err != nil { |
| fmt.Println("Listen is failed", err) |
| } |
| } |
| |
用 net/http 作为 http client 发起 get 请求
| func Req() { |
| resp, err := http.Get("https://www.cnblogs.com/") |
| if err != nil { |
| fmt.Println("Requests is failed") |
| } |
| defer resp.Body.Close() |
| body, _ := io.ReadAll(resp.Body) |
| fmt.Println(string(body)) |
| } |
| func ReqParser() { |
| |
| baseURL := "https://baijiahao.baidu.com/s" |
| |
| params := url.Values{} |
| params.Set("id", "1775182904014714278") |
| params.Set("wfr", "spider") |
| params.Set("for", "pc") |
| |
| u, err := url.ParseRequestURI(baseURL) |
| if err != nil { |
| fmt.Printf("host:%s is failed err:%s", u.Host, err) |
| } |
| u.RawQuery = params.Encode() |
| rawURL := u.String() |
| fmt.Println(rawURL) |
| |
| resp, err := http.Get(rawURL) |
| defer resp.Body.Close() |
| fmt.Println(resp.Header) |
| content, _ := io.ReadAll(resp.Body) |
| fmt.Println(string(content)) |
| } |
| |
| |
| func ReqParser01() { |
| |
| endpoint := "https://hanyu.baidu.com/shici/detail" |
| client := &http.Client{Timeout: time.Second * 3} |
| req, _ := http.NewRequest(http.MethodGet, endpoint, nil) |
| params := url.Values{} |
| params.Add("from", "aladdin") |
| params.Add("pid", "aab6b2ffc6a64abb8c7afb5ad1bad000") |
| req.URL.RawQuery = params.Encode() |
| req.Header.Add("User-Agent", "xxx-uuu") |
| resp, _ := client.Do(req) |
| defer resp.Body.Close() |
| byte, _ := io.ReadAll(resp.Body) |
| fmt.Println(string(byte)) |
| } |
用 net/http 作为 http client 发起 post 请求
| func reqPost() { |
| url := "http://172.22.143.197:8080/recv" |
| data := `{"name": "siri", "address": "HK"}` |
| |
| resp, _ := http.Post(url, "application/json", strings.NewReader(data)) |
| defer resp.Body.Close() |
| content, _ := io.ReadAll(resp.Body) |
| fmt.Println(string(content)) |
| fmt.Println(resp.Header) |
| } |
| |
| |
| func reqPost01() { |
| endpoint := "http://172.22.143.197:8080/recv" |
| data := struct { |
| name string |
| age int |
| }{ |
| name: "siri", |
| age: 16, |
| } |
| payload, _ := json.Marshal(data) |
| |
| http.Post(endpoint, "application/json", bytes.NewReader(payload)) |
| |
| } |
服务端接收 request.data
| func Receiver(w http.ResponseWriter, req *http.Request) { |
| defer req.Body.Close() |
| byte, err := io.ReadAll(req.Body) |
| if err != nil { |
| fmt.Printf("request data is failed!, err:%s", err) |
| return |
| } |
| fmt.Println(string(byte)) |
| |
| w.Header().Set("Content-Type", "application/json") |
| w.WriteHeader(http.StatusOK) |
| return |
| } |
| |
| func main() { |
| http.HandleFunc("/recv", Receiver) |
| err := http.ListenAndServe("0.0.0.0:8080", nil) |
| if err != nil { |
| fmt.Println("Listen is failed", err) |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统