zoukankan      html  css  js  c++  java
  • [GO]简单的http服务器和客户端的实现

    package main
    
    import (
        "net/http"
        "fmt"
    )
    
    func Hello(w http.ResponseWriter, r *http.Request) {
        fmt.Printf("handle hello")
        fmt.Fprintf(w, "hello")
    }
    
    func main() {
        http.HandleFunc("/", Hello)
        err := http.ListenAndServe("0.0.0.0:8000", nil)
        if err != nil {
            fmt.Println("http listen failed")
        }
    }

    然后使用浏览器浏览本地的8000端口就可以看到相应的网页内容

     再来个客户端的小样

    package main
    
    import (
        "net/http"
        "fmt"
        "io/ioutil"
    )
    
    func main() {
        //res, err := http.Get("https://www.baidu.com/")
        res, err := http.Get("http://localhost:8000")
        if err != nil {
            fmt.Println("get err:", err)
            return
        }
    
        data, _ := ioutil.ReadAll(res.Body)
        fmt.Println(string(data))
    }
  • 相关阅读:
    Git标签
    Git管理修改和撤销修改
    Git删除文件
    Git解决冲突
    Git的stash功能
    Git管理分支
    Git的多人协作模式
    Git相关指令
    LC7 整数翻转 + LC9 回文数
    LC1 两数之和
  • 原文地址:https://www.cnblogs.com/baylorqu/p/9844019.html
Copyright © 2011-2022 走看看