"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to efficiently merge the same type of Go structure? JSON encoding method

How to efficiently merge the same type of Go structure? JSON encoding method

Posted on 2025-04-16
Browse:846

How Can I Efficiently Merge Go Structs of the Same Type Using JSON Encoding?

Merge Fields of Identical Structures with JSON Encoding

In the realm of Go programming, the task of merging fields from two structures of the same type arises frequently. Consider a scenario where you have a default configuration, such as:

type Config struct {
  path string
  id   string
  key  string
  addr string
  size uint64
}

var DefaultConfig = Config{"", "", "", "", 0}

And a configuration loaded from a file, such as:

var FileConfig = Config{"", "file_id", "", "file_addr", 0}

Your objective is to merge these two configurations so that the result possesses the values from both structs, with FileConfig overwriting any values in DefaultConfig. However, FileConfig may not contain all fields.

Originally, you pondered utilizing reflection for this task:

func merge(default *Config, file *Config) (*Config) {
  b := reflect.ValueOf(default).Elem()
  o := reflect.ValueOf(file).Elem()

  for i := 0; i 

However, in this instance, reflection is not an optimal solution. A more elegant approach is to leverage the power of the encoding/json package.

The encoding/json package provides a simple mechanism to unmarshal JSON data into a predefined Go struct. Utilizing this technique, you can elegantly merge your configurations:

import (
  "encoding/json"
  "strings"
)

const fileContent = `{"id":"file_id","addr":"file_addr","size":100}`

func unmarshalConfig(conf *Config, content string) error {
  return json.NewDecoder(strings.NewReader(content)).Decode(conf)
}

func mergeConfigs(defConfig *Config, fileConfig *Config) error {
  if err := unmarshalConfig(defConfig, fileContent); err != nil {
    return err
  }
  
  for _, v := range fileConfig {
    defConfig[v.key] = v.value
  }
  
  return nil
}

In this solution, the fileConfig is unmarshaled into the default configuration. The encoding/json package handles all the complexities of setting field values, including missing values (which will default to their zero value) and file-specified values that override default values.

By utilizing unmarshaling, you achieve a simple and efficient solution to merge structures of the same type, ensuring that FileConfig fields that are set will take precedence over default values.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3