Mies.co blog

Last published on 17 Dec 2020
by Asten Mies

Go Struct Override

How to (partially) override values of a struct, based on a new struct. This snippet shows how to override only one of the colors of a default theme.

Run on the Go playground

package main

import (
	"fmt"
	"reflect"
)

type palette struct {
	Bg         string
	Fg         string
	ContrastBg string
	ContrastFg string
}

type theme struct {
	palette
}

func newTheme() *theme {
	t := &theme{}
	t.palette = palette{
		Fg:         "white",
		Bg:         "white",
		ContrastBg: "red",
		ContrastFg: "white",
	}

	return t
}

type paletteCustom struct {
	ContrastBg string
}

var paletteOverrides = paletteCustom{
	ContrastBg: "green",
}

func main() {
	th := newTheme()

	refValue := reflect.ValueOf(paletteOverrides)
	refType := refValue.Type()

	for i := 0; i < refValue.NumField(); i++ {
		fieldName := refType.Field(i).Name
		fieldValue := refValue.Field(i).Interface()

		value := reflect.ValueOf(th).Elem().FieldByName(fieldName)
		value.Set(reflect.ValueOf(fieldValue))
	}

	// Should output "green" if the override occured correctly
	fmt.Println(th.ContrastBg)
}