golang web服务器返回状态码 - Go语言中文社区

golang web服务器返回状态码


服务器:

package main

import (
 "net/http"
)

func main() {

 http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request){


   w.Header().Set("name", "my name is smallsoup")
   w.WriteHeader(500)
   w.Write([]byte("hello worldn"))

 })

 http.ListenAndServe(":8080", nil)
}

客户端:

package main

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

func main() {

 myHttpGet()

}

func myHttpGet() {

 rsp, err := http.Get("http://localhost:8080")
 if err != nil {
   fmt.Println("myHttpGet error is ", err)
   return
 }

 defer rsp.Body.Close()
 body, err := ioutil.ReadAll(rsp.Body)
 if err != nil {
   fmt.Println("myHttpGet error is ", err)
   return
 }

 fmt.Println("response statuscode is ", rsp.StatusCode, 
         "nhead[name]=", rsp.Header["Name"], 
           "nbody is ", string(body))
}

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/chenchongg/article/details/85679441
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-18 06:24:37
  • 阅读 ( 973 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢