Using dasel as a go package

Getting a root node

You can convert values into something usable by dasel with dasel.ValueOf.

myValue := map[string]any{
    "name": "Tom"
}
rootNode := dasel.ValueOf(myValue)

It's worth mentioning that this step isn't required because the commands listed below will do this conversion internally.

Running commands

func main() {
	myValue := map[string]any{
		"firstName": "Tom",
		"lastName":  "Wright",
	}
	values, err := dasel.Select(myValue, "firstName")
	if err != nil {
		log.Fatalf("could not select: %s", err)
	}

	results := values.Interfaces()
	stringResults := make([]string, len(results))
	for k, v := range results {
		stringResults[k] = fmt.Sprint(v)
	}
	fmt.Printf("select result: %s", strings.Join(stringResults, ", "))
	// select result: Tom
}

func main() {
	myValue := map[string]any{
		"firstName": "Tom",
		"lastName":  "Wright",
	}
	result, err := dasel.Put(myValue, "firstName", "Hello")
	if err != nil {
		log.Fatalf("could not select: %s", err)
	}

	fmt.Printf("original value: %v\n", myValue)
	fmt.Printf("put result: %v", result.Interface())
	// original value: map[firstName:Tom lastName:Wright]
	// put result: map[firstName:Hello lastName:Wright]
}

Note that if you pass a pointer, the original value also gets updated.

func main() {
    myValue := map[string]any{
       "firstName": "Tom",
       "lastName":  "Wright",
    }
    result, err := dasel.Put(&myValue, "firstName", "Hello")
    if err != nil {
       log.Fatalf("could not select: %s", err)
    }

    fmt.Printf("original value: %v\n", myValue)
    fmt.Printf("put result: %v", result.Interface())
    // original value: map[firstName:Hello lastName:Wright]
    // put result: map[firstName:Hello lastName:Wright]
}

func main() {
	myValue := map[string]any{
		"firstName": "Tom",
		"lastName":  "Wright",
	}
	result, err := dasel.Delete(myValue, "firstName")
	if err != nil {
		log.Fatalf("could not select: %s", err)
	}

	fmt.Printf("original value: %v\n", myValue)
	fmt.Printf("put result: %v", result.Interface())
	// original value: map[firstName:Tom lastName:Wright]
	// put result: map[lastName:Wright]
}

Note that if you pass a pointer, the original value also gets updated.

func main() {
	myValue := map[string]any{
		"firstName": "Tom",
		"lastName":  "Wright",
	}
	result, err := dasel.Delete(&myValue, "firstName")
	if err != nil {
		log.Fatalf("could not select: %s", err)
	}

	fmt.Printf("original value: %v\n", myValue)
	fmt.Printf("put result: %v", result.Interface())
	// original value: map[lastName:Wright]
	// put result: map[lastName:Wright]
}

Last updated