duanli9569 2017-01-10 18:16 采纳率: 100%
浏览 60
已采纳

如何在GO的Conn中编写响应(类型Response)?

I'm making a webserver in go and I need to give the response form the website to the client. this in the part of my code where i receive the response:

            client := http.Client{}
            resp, err := client.Do(request)
            defer resp.Body.Close()

            if err != nil {
                log.Fatalln(err.Error())
            }

The Do(request) returns a Response type and I need to send this response to the client (conn). I saw a method in conn type the writes data in the connection but it only accept bytes and i couldn't convert the response in bytes. I need to send the body, status and headers,can the conn.Write([]bytes) do that? How can I send this response to my client?

  • 写回答

1条回答 默认 最新

  • dqvy87517 2017-01-10 18:37
    关注

    The http.Response has a Write method, which writes the contents of the response to an io.Writer in HTTP/1.X format.

    This will write everything in the response verbatim, so you may need to remove/modify headers first to suit your needs.

    err = resp.Write(conn)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥50 安装华大九天aether
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)