Skip to main content
Loading

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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aerospike

import "github.com/aerospike/aerospike-client-go/v6/types"

// 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.
CTProxy
)

// 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 "github.com/aerospike/aerospike-client-go/v6"
)

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 {
panic(err)
}
...
...
key, _ := as.NewKey(namespace, set, "key5")
wp := as.NewWritePolicy(0,0)
deleted, err := client.Delete(WritePolicy, key)
}