Skip to content

Instantly share code, notes, and snippets.

@olivere
Created July 27, 2015 09:45
Show Gist options
  • Save olivere/eaee7a20524ab2beba71 to your computer and use it in GitHub Desktop.
Save olivere/eaee7a20524ab2beba71 to your computer and use it in GitHub Desktop.
Elastic: Sharing a client
package main
import (
"log"
"sync"
elastic "gopkg.in/olivere/elastic.v2"
)
var (
clientInit sync.Once
client *elastic.Client
)
func Client() *elastic.Client {
clientInit.Do(func() {
c, err := elastic.NewClient()
if err != nil {
panic(err)
}
client = c
})
return client
}
func main() {
exists, err := Client().IndexExists("name").Do()
if err != nil {
log.Fatal(err)
}
if !exists {
log.Print("index does not exist")
} else {
log.Print("index already exists")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment