# How to close the connection using Go client?

**URL:** https://discuss.elastic.co/t/how-to-close-the-connection-using-go-client/350123
**Category:** Elasticsearch
**Tags:** language-clients
**Created:** [December 29, 2023, 11:18am UTC](https://discuss.elastic.co/t/how-to-close-the-connection-using-go-client/350123 "2023-12-29T11:18:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![elleWajexi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellewajexi/32/127875_2.png) [@elleWajexi](https://discuss.elastic.co/u/elleWajexi)
#### Post date: [December 29, 2023, 11:18am UTC](https://discuss.elastic.co/t/how-to-close-the-connection-using-go-client/350123/1 "2023-12-29T11:18:16Z")

</div>

I normally connect to an elastic server, save the connection to a global variable, and reuse it in my entire app.

```auto
var es *elasticsearch.Client
 
func elasticConnect() {
  cfg := elasticsearch.Config{
          CloudID: "my_cloud_id",
          APIKey: "API_KEY"
  }
  var err error  
  es, err = elasticsearch.NewClient(cfg)

}

```

but sometimes I need to connect to another elastic instance and replace the global variable with the new one.  
I want to know how close the old connection before making a new one (I looked in the [documentation](https://pkg.go.dev/github.com/elastic/go-elasticsearch/v8), but I couldn't find any method for closing the connections)

---

<div class="post-metadata">

### Author: ![Laurent\_Saint-Felix](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/laurent_saint-felix/32/129563_2.png) [@Laurent\_Saint-Felix](https://discuss.elastic.co/u/Laurent_Saint-Felix)
#### Post date: [January 8, 2024, 4:16pm UTC](https://discuss.elastic.co/t/how-to-close-the-connection-using-go-client/350123/2 "2024-01-08T16:16:43Z")

</div>

The Go client is stateless by nature hence why there is nothing like a close method since we do not keep a live connection to the server.  
The rough idea is the client uses the information you provide both by config and by using the API to craft an HTTP requests which is then handled by the runtime.

You can control the underlying `http.Transport` by injecting it into the client's configuration within `Transport` and use that to close idle connections, it would still be up to you to stop existing request and prevent new ones during the swap.  
You can find an example of this injection [in this example](https://github.com/elastic/go-elasticsearch/blob/61ffcd9c180e7beb47c23a8add5f73b317afbcec/elasticsearch_example_test.go#L54).

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 5, 2024, 4:17pm UTC](https://discuss.elastic.co/t/how-to-close-the-connection-using-go-client/350123/3 "2024-02-05T16:17:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
