How To Install And Setup The Go (Golang) Client For Elasticsearch

Introduction

How to install and setup the Go (Golang) client for Elasticsearch. The stable version for Golang is 1.12 as of April 2019

Installing Golang programming language

If Golang and the Elasticsearch client for Go are already installed, then skip this section. If not, then visit the downloads page for Golang and get the MSI installer for Windows, or just download the TAR source for Linux.

Installing Golang on macOS:

There is a package for macOS on the downloads page, but the easiest way to install Go is to use the Homebrew package manager for macOS:

brew update
brew install go


Setup the GOPATH on your system

The default GOPATH one UNIX-based systems like Linux or macOS is $HOME/go, and on Windows it will be %USERPROFILE%\go.

Create a GOPATH for the Elasticsearch project:

Use the pwd command to have the terminal return the current directory. Use that path in this command to add it to the

export GOPATH={THE_PWD_PATH}

If the Go compiler running on the server is version 1.11 or newer, you can just build the application outside of the GOPATH.


Running the go Golang command in terminal

If everything installed properly, then open up a terminal or command prompt window, type go, go version, or go help, and press Return:

Screenshot of a macOS terminal using the "brew install" command to install GoLang


Install the go-elasticsearch GoLang module for Elasticsearch:

Any GoLang library package that includes a go.mod file is called a ‘module’. To install the go-elasticsearch GoLang module

NOTE: The major version of the go-elasticsearch GoLang module must match the major version of the Elasticsearch cluster installed on the server (e.g. Use version 6.x of go-elasticsearch if the Elasticsearch cluster is version 6.x. Use a cURL request if you’re unsure what version of Elasticsearch is running:

curl -XGET "localhost:9200"

Make a cURL request to the port that Elasticsearch is running on to get information about the cluster:

Terminal of a cURL request getting an Elasticsearch cluster information


Setup a project for the GoLang Elasticsearch app

Create a new directory for your project with the mkdir command and cd into it:

mkdir elastic-app
cd elastic-app

Add the go-elasticsearch package to your go.mod file using the cat command to add the require statement. Change elastic-app to the proper name for the Elasticsearch application you’re building:

cat > go.mod <<-END
  module elastic-app
  require github.com/elastic/go-elasticsearch/v8 master
END

# make sure that version of the package is compatible with the Elasticsearch cluster

NOTE: Use another version besides require github.com/elastic/go-elasticsearch/v8 master, and even multiple Elasticsearch versions in the same project are supported.

If you use sudo to create the directory, or if the permissions for the folder are not 755, then use chmod to correct the permissions to avoid the go.mod: Permission denied error:

sudo chmod 755 elastic-app

At this point you will see that the GoLang compiler built a go.mod file in the directory for you.

Create the main.go script and use go run to execute

Create a new file called main.go and use an editor like nano to edit it:

package main

import (
  "log"

  "github.com/elastic/go-elasticsearch/v8"
  // use the correct version here
)

func main() {
  elastic, _ := elasticsearch.NewDefaultClient()
  log.Println(elasticsearch.Version)
  log.Println(elastic.Info())
}

Paste the package main text above into the main.go file and press CTRL+O and CTRL+X to save and exit: Screenshot of the nano text editor importing the go-elasticsearch package in a main.go file of a GoLang application

In a terminal, inside the application directory, use Go’s run command to test the connection to the Elasticsearch cluster:

go run main.go

Installing the ‘go-elasticsearch’ package in a GoLang directory:


Conclusion

WRITERS: Write about setting up a GoLang Elasticsearch project

Pilot the ObjectRocket platform free for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.