简体   繁体   中英

Deep merge order maps in Go

I am reading two YAML files, one is considered the "base" and one the "environment". I need to unmarshal them as ordered maps and merge them, retaining the order.

So if baseline looks like this:

key1: baseline
key2:
  subkey1: baseline
  subkey2:
    subsubkey1: baseline

And the environment looks like this:

key2:
  subkey2:
    subsubkey1: environment

I want the resultant map to look like this

key1: baseline
key2:
  subkey1: baseline
  subkey2:
    subsubkey1: environment

I have tried to use yaml.MapSlice{} but it is essentially an array of arrays and you cannot really merge that.

With Ruby I can use activesupport's deep_merge() and get the desired result and get similar using Python's Collections.OrderedDict. Finding anything in Go has been difficult. I get people dismissing it by saying Go is designed this way.

That's all well and good, but I need the functionality for my project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM