Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么golang中fasthttp的func Get有'dst'参数?_Http_Go_Get - Fatal编程技术网

为什么golang中fasthttp的func Get有'dst'参数?

为什么golang中fasthttp的func Get有'dst'参数?,http,go,get,Http,Go,Get,我发现fasthttp godoc是一名研究员: func Get func Get(dst []byte, url string) (statusCode int, body []byte, err error) Get appends url contents to dst and returns it as body. The function follows redirects. Use Do* for manually handling redirects. New body buff

我发现fasthttp godoc是一名研究员:

func Get
func Get(dst []byte, url string) (statusCode int, body []byte, err error)
Get appends url contents to dst and returns it as body.
The function follows redirects. Use Do* for manually handling redirects.
New body buffer is allocated if dst is nil.
但是,当我运行同事代码时

package main

import (
    "fmt"
    fh "github.com/valyala/fasthttp"
)

func main() {
    url := "https://www.okcoin.cn/api/v1/ticker.do?symbol=btc_cny"

    dst := []byte("ok100")
    _, body, err := fh.Get(dst, url)

    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("body:", string(body))
    fmt.Println("dst:", string(dst))
}
body没有“ok100”,并且dst仍然是“ok100”。
为什么?

查看fasthttp中使用的代码,您可以看到,如果有超时,dst的内容将复制到第712行的正文中。因此,根据我在代码中读到的内容(并使用您的代码使用Delve进行调试),我发现dst在这种用法中没有得到更新。它似乎可以用来在超时的情况下为body提供默认内容——可能值得向fasthttp的作者直接询问更多细节

func clientGetURLDeadlineFreeConn(dst []byte, url string, deadline time.Time, c clientDoer) (statusCode int, body []byte, err error) {
timeout := -time.Since(deadline)
if timeout <= 0 {
    return 0, dst, ErrTimeout
}

var ch chan clientURLResponse
chv := clientURLResponseChPool.Get()
if chv == nil {
    chv = make(chan clientURLResponse, 1)
}
ch = chv.(chan clientURLResponse)

req := AcquireRequest()

// Note that the request continues execution on ErrTimeout until
// client-specific ReadTimeout exceeds. This helps limiting load
// on slow hosts by MaxConns* concurrent requests.
//
// Without this 'hack' the load on slow host could exceed MaxConns*
// concurrent requests, since timed out requests on client side
// usually continue execution on the host.
go func() {
    statusCodeCopy, bodyCopy, errCopy := doRequestFollowRedirects(req, dst, url, c)
    ch <- clientURLResponse{
        statusCode: statusCodeCopy,
        body:       bodyCopy,
        err:        errCopy,
    }
}()

tc := acquireTimer(timeout)
select {
case resp := <-ch:
    ReleaseRequest(req)
    clientURLResponseChPool.Put(chv)
    statusCode = resp.statusCode
    body = resp.body
    err = resp.err
case <-tc.C:
    body = dst
    err = ErrTimeout
}
releaseTimer(tc)

return statusCode, body, err

}

查看fasthttp中使用的代码,您可以看到,如果有超时,dst的内容将复制到第712行的正文中。因此,根据我在代码中读到的内容(并使用您的代码使用Delve进行调试),我发现dst在这种用法中没有得到更新。它似乎可以用来在超时的情况下为body提供默认内容——可能值得向fasthttp的作者直接询问更多细节

func clientGetURLDeadlineFreeConn(dst []byte, url string, deadline time.Time, c clientDoer) (statusCode int, body []byte, err error) {
timeout := -time.Since(deadline)
if timeout <= 0 {
    return 0, dst, ErrTimeout
}

var ch chan clientURLResponse
chv := clientURLResponseChPool.Get()
if chv == nil {
    chv = make(chan clientURLResponse, 1)
}
ch = chv.(chan clientURLResponse)

req := AcquireRequest()

// Note that the request continues execution on ErrTimeout until
// client-specific ReadTimeout exceeds. This helps limiting load
// on slow hosts by MaxConns* concurrent requests.
//
// Without this 'hack' the load on slow host could exceed MaxConns*
// concurrent requests, since timed out requests on client side
// usually continue execution on the host.
go func() {
    statusCodeCopy, bodyCopy, errCopy := doRequestFollowRedirects(req, dst, url, c)
    ch <- clientURLResponse{
        statusCode: statusCodeCopy,
        body:       bodyCopy,
        err:        errCopy,
    }
}()

tc := acquireTimer(timeout)
select {
case resp := <-ch:
    ReleaseRequest(req)
    clientURLResponseChPool.Put(chv)
    statusCode = resp.statusCode
    body = resp.body
    err = resp.err
case <-tc.C:
    body = dst
    err = ErrTimeout
}
releaseTimer(tc)

return statusCode, body, err
}