---
title: Insert
summary: Insert creates a WriteCloseAbandoner used for inserting an object into the cache for the given key. For the insertion to complete successfully, the object must be fully written to the writer and its Close method called.
url: https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/cache/core/fn.Insert/
---

**Kind:** Function  
**Module:** [core](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/cache/core/)  
**Package:** compute-sdk-go v1.11.0  
**Source:** https://github.com/fastly/compute-sdk-go/blob/d6bf71b04516ed72d6018cab6fff701a865f86e5/cache/core/core.go#L482

```go
func Insert(key []byte, opts WriteOptions) (WriteCloseAbandoner, error)
```

Insert creates a [WriteCloseAbandoner](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/cache/core/interface.WriteCloseAbandoner/) used for inserting an object
into the cache for the given key.  For the insertion to complete
successfully, the object must be fully written to the writer and its
Close method called.

If Close is not called, or the writer's Abandon method is called, the
insertion is incomplete and any concurrent lookups that may be
reading from the object as it is streamed into the cache may
encounter an error while reading.

Unlike [Transaction.Insert](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/cache/core/struct.Transaction/#method-Insert), this does not coordinate with any other
lookups or inserts for this key.  Concurrent inserts may race with
concurrent lookups or insertions, and will unconditionally overwrite
existing cached items rather than allowing for revalidation of an
existing object.

**Parameters**

- `key`: `[]byte`
- `opts`: `WriteOptions`

**Returns**

`(WriteCloseAbandoner, error)`

```go
const (
	key      = "my_key"
	contents = "my cached object"
)

w, err := core.Insert([]byte(key), core.WriteOptions{
	TTL:           time.Hour,
	SurrogateKeys: []string{key},
	Length:        uint64(len(contents)),
})
if err != nil {
	panic(err)
}

if _, err := io.WriteString(w, contents); err != nil {
	panic(err)
}

if err := w.Close(); err != nil {
	panic(err)
}
```
