How to Connect to Elasticsearch from Go

Connect to Elasticsearch from Go using the official elastic/go-elasticsearch client library with a minimal configuration example.

Use the official elastic/go-elasticsearch client to connect to Elasticsearch from Go.

package main

import (
	"context"
	"log"

	"github.com/elastic/go-elasticsearch/v8"
)

func main() {
	es, err := elasticsearch.NewClient(elasticsearch.Config{
		Addresses: []string{"http://localhost:9200"},
	})
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	res, err := es.Info(ctx)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(res.String())
}