使用Go通过一个tls连接进行Http GET请求

Currently, I am using http GET requests to connect to a server. Get request is calling from inside a thread. For each Get request one thread is being used but problem is for each Get request, one connection is established. Thus if there are 10000 Get request then 10000 connections will be established. However, I want first to establishe a TLS connection between me and the server, then create a thread and from that thread I want to send Get over that already established connection.

e.g.

for {
    1. establish a tls connection
    2. create thread go func()
}

func() {
    resp, err := http.Get(url) // should be over already established tls connection
}

Firstly, just to clarify, Go has goroutines, not threads. Goroutines are co-operative light-weight processes. The Go runtime time-slices a number of goroutines onto the available operating system threads. This thread abstraction is an important feature of Go.

On the question of making many concurrent GET requests using goroutines, it is best initially to let the standard API handle multiplexing of requests onto connections for you. There may be a large number of requests and a smaller number of supporting connections. Provided you use keep-alive connections, you should not need to care about the details.

For control over proxies, TLS configuration, keep-alives, compression, and other settings, create a Transport

HTTP/1.1 handles keep-alive connections with both http and https. This is a benefit when many requests are made to the same server. As long as you don't force it to close each connection after each request, you will get a lot of benefit from keep-alive connections in your case.

The usual mantra applies: don't optimise prematurely. Describe what you need to do as clearly as possible. Benchmark it (Go has a useful micro-benchmark tool). Do this before you decide whether or how to optimise the performance.