dotpath
Module in GoThe dotpath
module in Go provides a convenient way to
access nested data structures using dot-notated paths. This is
particularly useful when working with JSON-like data or complex nested
maps and slices. This tutorial will guide you through using the
dotpath
module, including example code and usage
scenarios.
The dotpath
module includes two primary functions: -
GetDotpath(obj interface{}, path string) (interface{}, bool)
:
Retrieves a value from a nested structure using a dot-notated path. -
HasDotpath(obj interface{}, path string) bool
: Checks if a
given path exists in a nested structure.
These functions allow you to easily navigate and query complex data structures.
To use the dotpath
module, you need to have it in your
Go workspace.
go get github.com/caltechlibrary/dotpath
You import the dotpath
module with
import "github.com/caltechlibrary/dotpath"
For this tutorial, we’ll assume you have the dotpath
package available in your project directory.
Let’s consider the following nested data structure, which we’ll use in our examples:
:= map[string]interface{}{
data "user": map[string]interface{}{
"profile": map[string]interface{}{
"name": "Alice",
"hobbies": []interface{}{"reading", "hiking", "coding"},
},
"settings": map[string]interface{}{
"notifications": []interface{}{"email", "sms"},
"theme": "dark",
},
},
"stats": []interface{}{
map[string]interface{}{"metric": "visits", "value": 1000},
map[string]interface{}{"metric": "signups", "value": 250},
},
}
GetDotpath
The GetDotpath
function allows you to retrieve values
from a nested map or slice using a dot-notated path. Here’s how you can
use it:
package main
import (
"fmt"
"github.com/caltechlibrary/dotpath"
)
func main() {
:= map[string]interface{}{
data "user": map[string]interface{}{
"profile": map[string]interface{}{
"name": "Alice",
"hobbies": []interface{}{"reading", "hiking", "coding"},
},
"settings": map[string]interface{}{
"notifications": []interface{}{"email", "sms"},
"theme": "dark",
},
},
"stats": []interface{}{
map[string]interface{}{"metric": "visits", "value": 1000},
map[string]interface{}{"metric": "signups", "value": 250},
},
}
// Retrieve the user's name
if value, ok := dotpath.GetDotpath(data, "user.profile.name"); ok {
.Println("User's name:", value)
fmt} else {
.Println("Path not found")
fmt}
// Retrieve the second hobby
if value, ok := dotpath.GetDotpath(data, "user.profile.hobbies[1]"); ok {
.Println("Second hobby:", value)
fmt} else {
.Println("Path not found")
fmt}
// Retrieve the value of signups
if value, ok := dotpath.GetDotpath(data, "stats[1].value"); ok {
.Println("Signups value:", value)
fmt} else {
.Println("Path not found")
fmt}
}
hobbies[1]
).GetDotpath
returns the
value at the specified path and a boolean indicating success.HasDotpath
The HasDotpath
function checks if a given path exists in
the nested structure. This is useful for validating paths before
attempting to retrieve values.
package main
import (
"fmt"
"github.com/caltechlibrary/dotpath" // Adjust the import path as needed
)
func main() {
:= map[string]interface{}{
data "user": map[string]interface{}{
"profile": map[string]interface{}{
"name": "Alice",
"hobbies": []interface{}{"reading", "hiking", "coding"},
},
"settings": map[string]interface{}{
"notifications": []interface{}{"email", "sms"},
"theme": "dark",
},
},
"stats": []interface{}{
map[string]interface{}{"metric": "visits", "value": 1000},
map[string]interface{}{"metric": "signups", "value": 250},
},
}
// Check if the path to the user's name exists
if dotpath.HasDotpath(data, "user.profile.name") {
.Println("Path 'user.profile.name' exists")
fmt} else {
.Println("Path 'user.profile.name' does not exist")
fmt}
// Check if the path to a non-existent key exists
if dotpath.HasDotpath(data, "user.profile.age") {
.Println("Path 'user.profile.age' exists")
fmt} else {
.Println("Path 'user.profile.age' does not exist")
fmt}
}
HasDotpath
to
check if a path exists before attempting to retrieve its value.HasDotpath
returns
true
if the path exists, false
otherwise.