package main

import (
	"fmt"
	"slices"
)

var config = map[string]string{
	"host":     "localhost",
	"port":     "8080",
	"username": "admin",
	"password": "secret",
}

// section: unordered
func printConfig(cfg map[string]string) {
	for key, value := range cfg {
		fmt.Printf("%s: %s\n", key, value)
	}
}

// section: unordered

// section: sorted
func printConfigSorted(cfg map[string]string) {
	keys := make([]string, 0, len(cfg))
	for key := range cfg {
		keys = append(keys, key)
	}
	slices.Sort(keys)

	for _, key := range keys {
		fmt.Printf("%s: %s\n", key, cfg[key])
	}
}

// section: sorted

// section: main
func main() {
	fmt.Println("Unordered run 1:")
	printConfig(config)

	fmt.Println("\nUnordered run 2:")
	printConfig(config)

	fmt.Println("\nSorted iteration:")
	printConfigSorted(config)
}

// section: main

/*
// section: output
# Output captured with GODEBUG=randmapiter=1 (order will vary each run)
Unordered run 1:
host: localhost
port: 8080
username: admin
password: secret

Unordered run 2:
password: secret
host: localhost
port: 8080
username: admin

Sorted iteration:
host: localhost
password: secret
port: 8080
username: admin
// section: output
*/
