Hazelcast Go Client

Hazelcast is a feature-rich and developer-friendly in-memory caching solution for your Go applications.

  • Access Hazelcast data structures plus distributed queues, topics, and more with the Hazelcast Go Client.
  • Declarative configuration support via JSON.
  • SQL support.
  • High-performance aggregation functions, such as sum, average, max, and min, for Hazelcast Map entries. They can run in parallel for each partition and are highly optimized for speed and low-memory consumption.

Quick Start + Download

Quick Start

Get Hazelcast.

Use the following command to install the Hazelcast Go client:

go get github.com/hazelcast/hazelcast-go-client

Next steps:

 

Downloads

Version Downloads Documentation
Hazelcast IMDG Go Client 1.4.1 (latest)
08/22/2023
Hazelcast IMDG Go Client 1.4.0
03/17/2023
Hazelcast IMDG Go Client 1.3.2
02/10/2023
Hazelcast IMDG Go Client 1.2.0
04/25/2024

For all downloads please see the Download Archives

Previous Releases

Code Samples

package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the map.
	myMap, err := client.GetMap(ctx, "my-map")
	if err != nil {
		panic(err)
	}
	// Set a value in the map.
	err = myMap.Set(ctx, "some-key", "some-value")
	if err != nil {
		panic(err)
	}
	// Get the value back and print it.
	value, err := myMap.Get(ctx, "some-key")
	if err != nil {
		panic(err)
	}
	fmt.Println("Got the value back:", value)
}
package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the replicated map.
	myMap, err := client.GetReplicatedMap(ctx, "my-replicated-map")
	if err != nil {
		panic(err)
	}
	// Put a value in the map and retrieve the previous value.
	oldValue, err := myMap.Put(ctx, "some-key", "some-value")
	if err != nil {
		panic(err)
	}
	fmt.Println("Previous value:", oldValue)
	// Get the value back and print it.
	value, err := myMap.Get(ctx, "some-key")
	if err != nil {
		panic(err)
	}
	fmt.Println("Got the value back:", value)
}
package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the multi map.
	myMap, err := client.GetMultiMap(ctx, "my-multi-map")
	if err != nil {
		panic(err)
	}
	// Put values in the map with the same key.
	err = myMap.PutAll(ctx, "some-key", "some-value-1", "some-value-2", "some-value-3")
	if err != nil {
		panic(err)
	}
	// Get the values back and print it.
	values, err := myMap.Get(ctx, "some-key")
	if err != nil {
		panic(err)
	}
	fmt.Println("Got the values back:", values)
}
package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the set.
	mySet, err := client.GetSet(ctx, "my-set")
	if err != nil {
		panic(err)
	}
	// Add items to set.
	// Note that value1 is added twice.
	changed, err := mySet.AddAll(ctx, "value1", "value2", "value1", "value3")
	if err != nil {
		panic(err)
	}
	fmt.Println("Was the set changed?", changed)
	// Print contents of the set
	items, err := mySet.GetAll(ctx)
	if err != nil {
		panic(err)
	}
	// Get the items back and print them.
	fmt.Println("Got the items back:", items)
}
package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the list.
	myList, err := client.GetList(ctx, "my-list")
	if err != nil {
		panic(err)
	}
	_, err = myList.AddAll(ctx, "tic", "toc")
	if err != nil {
		panic(err)
	}
	// Get and print list items.
	items, err := myList.GetAll(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println("List items:", items)
} 
package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the queue.
	myQueue, err := client.GetQueue(ctx, "my-queue")
	if err != nil {
		panic(err)
	}
	// Add an item to the queue if space is available (non-blocking).
	added, err := myQueue.Add(ctx, "item 1")
	if err != nil {
		panic(err)
	}
	fmt.Println("Added?", added)
	// Get the head of the queue if available and print item.
	item, err := myQueue.Poll(ctx)
	if err != nil {
		panic(err)
	}
	fmt.Println("Got item:", item)
}
package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
)

// messageListener handles incoming messages to the topic.
func messageListener(event *hazelcast.MessagePublished) {
	fmt.Println("Received message: ", event.Value)
}

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the queue.
	myTopic, err := client.GetTopic(ctx, "my-topic")
	if err != nil {
		panic(err)
	}
	// Add a message listener to the topic.
	_, err = myTopic.AddMessageListener(ctx, messageListener)
	if err != nil {
		panic(err)
	}
	// Publish messages to topic
	for i := 0; i < 10; i++ {
		err = myTopic.Publish(ctx, fmt.Sprintf("Message %d", i))
		if err != nil {
			panic(err)
		}
	}
	// Shutdown the client.
	client.Shutdown(ctx)
}
package main

import (
	"context"
	"fmt"

	"github.com/hazelcast/hazelcast-go-client"
	"github.com/hazelcast/hazelcast-go-client/predicate"
	"github.com/hazelcast/hazelcast-go-client/serialization"
)

func main() {
	ctx := context.TODO()
	// Start the client with defaults.
	client, err := hazelcast.StartNewClient(ctx)
	if err != nil {
		panic(err)
	}
	// Get a reference to the map.
	myMap, err := client.GetMap(ctx, "my-map")
	if err != nil {
		panic(err)
	}
	// Populate the map, error handling is omitted here for brevity
	_ = myMap.Set(ctx, "key-1", serialization.JSON(`{"property: 5}`))
	_ = myMap.Set(ctx, "key-2", serialization.JSON(`{"property": 10}`))
	_ = myMap.Set(ctx, "key-3", serialization.JSON(`{"property": 15}`))
	// Filter the entries in the map based on a predicate and print those.
	pred := predicate.And(predicate.Greater("property", 8), predicate.Less("property", 12))
	entries, err := myMap.GetEntrySetWithPredicate(ctx, pred)
	if err != nil {
		panic(err)
	}
	fmt.Println(entries)
}