---
title: "Read"
description: "Guide to reading records with the Aerospike Go client, including Get, Exists, GetHeader, and custom read policies."
---

# Read

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

Jump to the [Code block](#code-block) for a combined complete example.

## Setup

The following examples use the setup and record below to illustrate single record data retrieval from an Aerospike database.

```go
import (

    "fmt"

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

)

// Establishes a connection to the server

client, err := aerospike.NewClient("127.0.0.1", 3000)

if err != nil {

    log.Fatal(err)

}

defer client.Close()

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5001

key, err := aerospike.NewKey("sandbox", "ufodata", 5001)

if err != nil {

    log.Fatal(err)

}
```

The record structure:

```plaintext
+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+

| PK   | occurred | reported | posted   | report                                                                                                                                                                                                                 | location                                                   |

+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+

| 5001 | 20220531 | 20220601 | 20220601 | MAP('{"shape":["circle", "flash", "disc"], "summary":"Large flying disc flashed in the sky above the student union. Craziest thing I've ever seen!", "city":"Ann Arbor", "state":"Michigan", "duration":"5 minutes"}') | GeoJSON('{"type":"Point","coordinates":[42.2808,83.743]}') |

+------+----------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------+
```

## Policies

Instead of using the default read [policies](https://aerospike.com/docs/database/learn/policies), we can set them on a per command basis.

The following example creates a new read policy and sets the [socket timeout](https://aerospike.com/docs/database/learn/policies#socket-timeout) for the command.

```go
// Create new read policy

policy := aerospike.NewPolicy()

policy.SocketTimeout = 300
```

## Read a record

### Record exists

Checking record existence is faster than getting the record because it only needs to look at the primary index.

```go
// Returns true if exists, false if not

exists, err := client.Exists(policy, key)

if err != nil {

    log.Fatal(err)

}

// Do something

fmt.Printf("Exists: %v", exists)
```

### Record metadata only

Read record metadata (generation and expiration information) without reading the bins for a specified key.

```go
// Get record metadata

record, err := client.GetHeader(policy, key)

if err != nil {

    log.Fatal(err)

}

// Do something

fmt.Printf("Record: %v", record)
```

### Whole record

Read the record metadata and all bins for a specified key.

```go
// Get whole record

record, err := client.Get(policy, key)

if err != nil {

    log.Fatal(err)

}

// Do something

fmt.Printf("Record: %v", record.Bins)
```

### Specific bins

Read the record metadata and the `report` and `location` bins from the record.

```go
// Get bins 'report' and 'location'

record, err := client.Get(policy, key, "report", "location")

if err != nil {

    log.Fatal(err)

}

// Do something

fmt.Printf("Record: %v", record.Bins)
```

## Code block

Expand this section for a single code block to read a record

```go
import (

    "fmt"

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

)

func main() {

    // Establishes a connection to the server

    client, err := aerospike.NewClient("127.0.0.1", 3000)

    if err != nil {

        log.Fatal(err)

    }

    defer client.Close()

    // Creates a key with the namespace "sandbox", set "ufodata", and user key 5001

    key, err := aerospike.NewKey("sandbox", "ufodata", 5001)

    if err != nil {

        log.Fatal(err)

    }

    // Create new read policy

    policy := aerospike.NewPolicy()

    policy.SocketTimeout = 300

    // Get whole record

    record, err := client.Get(policy, key)

    if err != nil {

        log.Fatal(err)

    }

    // Do something

    fmt.Printf("Record: %v", record.Bins)

}
```