Skip to main content

COPY

Syntax

COPY source destination [DB destination-db] [REPLACE]

Module

generic

Categories

slow write keyspace

Description

Copies the value stored at the source key to the destination key. Returns 1 if copied and 0 if not copied. Also returns 0 if the destination key already exists in the database and the REPLACE option is not set.

Options

  • DB destination-db: the destination database to copy the key to
  • REPLACE: replace the destination key if it already exists

Examples

The API provides a struct called COPYOptions that wraps these options in a convenient object.

     type COPYOptions struct {        
Database string
Replace bool
}

Copy the value stored at key 'hello' to the new key 'bye'

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
db.Set("hello", "world")
key = db.Copy("hello", "bye")

Copy the value stored at key 'hello' in database 0 and replace the value at key 'bye' in database 1

db, err := sugardb.NewSugarDB()
if err != nil {
log.Fatal(err)
}
err := db.SelectDB(1)
ok, err := db.Set("bye", "goodbye")
err := db.SelectDB(0)
ok, err := db.Set("hello", "world")
ret, err = db.Copy("hello", "bye", db.COPYOptions{Database: "1", Replace: true})