Skip to main content

main.go

main.go - Overview

This file provides a basic "Hello, World!" example using the DiceDB Go client to connect to a DiceDB server, set a key-value pair, and retrieve it.

Detailed Documentation

main function

Purpose: The main entry point of the program. It connects to a DiceDB server, sets a key-value pair, and retrieves the value associated with the key.

  • Connects to the DiceDB server at localhost:7379.
  • Sets the key k1 to the value v1.
  • Retrieves the value associated with the key k1.
  • Prints the results to the console.
package main

import (
"fmt"

"github.com/dicedb/dicedb-go"
"github.com/dicedb/dicedb-go/wire"
)

func main() {
// create a new DiceDB client and connect to the server
client, err := dicedb.NewClient("localhost", 7379)
if err != nil {
panic(err)
}
defer client.Close()

// define a key and value
key := "k1"
value := "v1"

// set the key and value
resp := client.Fire(&wire.Command{
Cmd: "SET",
Args: []string{key, value},
})
if resp.Err != "" {
fmt.Println("error setting key:", resp.Err)
return
}
fmt.Printf("successfully set key %s=%s\n", key, value)

// get the key and value
resp = client.Fire(&wire.Command{
Cmd: "GET",
Args: []string{key},
})
if resp.Err != "" {
fmt.Println("error getting key:", resp.Err)
return
}

fmt.Printf("successfully got key %s=%s\n", key, resp.GetVStr())
}

Getting Started Relevance