悉野小楼

导航

go学习笔记10(HTTP)

http服务.go

package main

import (
    "fmt"
    "net/http"
)

func PageHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.RequestURI)
    w.Write([]byte("hello world"))
}

func main() {
    http.HandleFunc("/", PageHandler)
    http.ListenAndServe(":8087", nil)
}

 

http请求.go

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    res, err := http.Get("http://localhost:8087")
    if err != nil {
        fmt.Println("err:", err)
        return
    }
    defer res.Body.Close()
    resBody, err := ioutil.ReadAll(res.Body)
    fmt.Println("client receive:" + string(resBody))
}

 

posted on 2026-04-16 20:39  悉野  阅读(10)  评论(0)    收藏  举报