Http 将标志设置为golang请求

Http 将标志设置为golang请求,http,go,curl,request,Http,Go,Curl,Request,我有一个CURL字符串CURL--unixsocket/var/run/docker.sockhttp://localhost/containers/json 它有一个标志--unix sock 如何在golang中将此标志设置为http请求?使用连接到unix套接字(为简洁起见,省略了大多数错误处理): package main import ( "context" "fmt" "log" "net" "net/http" "net/http/

我有一个CURL字符串
CURL--unixsocket/var/run/docker.sockhttp://localhost/containers/json
它有一个标志--unix sock 如何在golang中将此标志设置为http请求?

使用连接到unix套接字(为简洁起见,省略了大多数错误处理):

package main

import (
    "context"
    "fmt"
    "log"
    "net"
    "net/http"
    "net/http/httputil"
)

func main() {
    c := &http.Client{
        Transport: &http.Transport{
            DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
                return (&net.Dialer{}).DialContext(ctx, "unix", "/var/run/docker.sock")
            },
        },
    }

    req, _ := http.NewRequest("GET", "http://localhost/containers/json", nil)

    res, err := c.Do(req)
    if err != nil {
        log.Fatal(err)
    }

    b, _ := httputil.DumpResponse(res, true)
    fmt.Println(string(b))
}