从Yaml文件中以自定义格式读取配置

I have a configuration file in YAML format. I am trying to read the configuration in some custom format. I couldn't guess any pattern that I can go for like tree, json etc. Eg. application.yaml

organization:
  products:
    product1:
      manager: "Rob"
      engineer: "John"
    product2:
      manager: "Henry"
      lead: "patrick"

The configuration file can have huge information and that can vary from file to file. I want to construct data in the following format,

organization/products/product1/manager  =  Rob
organization/products/product1/engineer = John
organization/products/product2/lead     = patrick

OR

{"organization/products/product1/manager":"Rob","organization/products/product2/lead":"patrick"}

Any idea how I can achieve this pattern?

This is essentially an exercise in printing trees. The exact implementation will depend on the particular YAML parser you pick, but pretty much all of them will have some kind of "map of anything" type. In the very popular gopkg.in/yaml.v2 this type is named MapSlice (don't let the name confuse you; it leaks its implementation which has to deal with flexible key types).

Just throw it at your favorite tree traversal algorithm to render the text file. Here is a simple example that works with only string keys and only some scalar leaf nodes:

package main

import (
    "bytes"
    "fmt"
    "io"
    "log"
    "path/filepath"
)

func main() {
    var tree yaml.MapSlice
    if err := yaml.Unmarshal(input, &tree); err != nil {
        log.Fatal(err)
    }

    var buf bytes.Buffer
    if err := render(&buf, tree, ""); err != nil {
        log.Fatal(err)
    }
}

func render(w io.Writer, tree yaml.MapSlice, prefix string) error {
    for _, branch := range tree {
        key, ok := branch.Key.(string)
        if !ok {
            return fmt.Errorf("unsupported key type: %T", branch.Key)
        }

        prefix := filepath.Join(prefix, key)

        switch x := branch.Value.(type) {
        default:
            return fmt.Errorf("unsupported value type: %T", branch.Value)

        case yaml.MapSlice:
            // recurse
            if err := render(w, x, prefix); err != nil {
                return err
            }
            continue

        // scalar values
        case string:
        case int:
        case float64:
        // ...
        }

        // print scalar
        if _, err := fmt.Fprintf(w, "%s = %v
", prefix, branch.Value); err != nil {
            return err
        }
    }

    return nil
}