Skip to main content

Go Client

Client applications which connect to Aerospike Cloud instances with the Go client library can use a client builder function that takes a ClientType argument of either CTNative (for non-Cloud applications) or CTProxy (for Cloud applications) along with policy and host arguments. The builder function returns either a native client or a proxy client that adheres to the ClientIfc interface.

// Copyright 2014-2022 Aerospike, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package aerospike

import ""

// ClientType determines the type of client to build.
type ClientType int

const (
// CTNative means: Create a native client.
CTNative ClientType = iota

// CTProxy means: Create a proxy client.

// CreateClientWithPolicyAndHost generates a new Client of the specified type
// with the specified ClientPolicy and sets up the cluster using the provided hosts.
// If the policy is nil, the default relevant policy will be used.
func CreateClientWithPolicyAndHost(typ ClientType, policy *ClientPolicy, hosts ...*Host) (ClientIfc, Error) {
if len(hosts) == 0 {
return nil, newError(types.SERVER_NOT_AVAILABLE, "No hosts were provided")

switch typ {
case CTNative:
return NewClientWithPolicyAndHost(policy, hosts...)
case CTProxy:
if len(hosts) > 1 {
return nil, newError(types.GRPC_ERROR, "Only one proxy host is acceptable")
return NewProxyClientWithPolicyAndHost(policy, hosts[0])
return nil, newError(types.SERVER_NOT_AVAILABLE, "Invalid client type")

To create the client object, call the CreateClientWithPolicyAndHost function and use the returned client interface as usual.

Replace the placeholder values in the following code with the correct values from your Aerospike Cloud instance.

package main

import (
as ""

func main() {
user := "user"
password := "password"
host := "localhost"
port := 3000

cp := as.NewClientPolicy()
cp.User = user
cp.Password = password
cp.Timeout = 3 * time.Second

ash := as.NewHost(host, port)
client, err := as.CreateClientWithPolicyAndHost(as.CTNative, cp, ash)
if err != nil {
key, _ := as.NewKey(namespace, set, "key5")
wp := as.NewWritePolicy(0,0)
deleted, err := client.Delete(WritePolicy, key)