Deep Merge Configuration Maps

Merge layered JSON-like configuration maps recursively so nested objects combine while scalar overrides replace base values.

GoConfig Management

Code

gosnippet
39 lines
package main

import (
	"encoding/json"
	"fmt"
)

func Merge(dst, src map[string]any) map[string]any {
	if dst == nil {
		dst = make(map[string]any)
	}
	for key, value := range src {
		if incoming, ok := value.(map[string]any); ok {
			if existing, ok := dst[key].(map[string]any); ok {
				dst[key] = Merge(existing, incoming)
				continue
			}
			dst[key] = Merge(nil, incoming)
			continue
		}
		dst[key] = value
	}
	return dst
}

func main() {
	base := map[string]any{
		"server": map[string]any{"port": 8080.0, "tls": false},
		"name":   "api",
	}
	override := map[string]any{
		"server": map[string]any{"port": 9090.0},
		"debug":  true,
	}
	merged := Merge(base, override)
	encoded, _ := json.Marshal(merged)
	fmt.Println(string(encoded))
}

How it works

Everything happens inside the functions Merge and main.

Repetition is confined to for key, value := range src — the rest of the file is straight-line.

What to watch for

  • The environment needs no preparation beyond having the interpreter or compiler.

Applying it

The main signature reads func Merge(dst, src map[string]any) map[string]any

Execute the file and main walks through Merge, in that order.

Statements that matter

for key, value := range src {
if incoming, ok := value.(map[string]any); ok {
if existing, ok := dst[key].(map[string]any); ok {
dst[key] = Merge(existing, incoming)
continue
}
dst[key] = Merge(nil, incoming)
continue
}
dst[key] = value
}
dst = make(map[string]any)

Interactive Tools for This Topic

FAQ

How do you run Deep Merge Configuration Maps?
The runtime alone is enough: execute the file as a complete program.