defaults icon indicating copy to clipboard operation
defaults copied to clipboard

Set does not report errors

Open licaonfee opened this issue 1 year ago • 1 comments

Given the following code

package main

import (
	"fmt"

	"github.com/creasty/defaults"
)

type Kitten struct {
	MaxHP int `default:"powerfull"`
}

func main() {
	var k Kitten
	if err := defaults.Set(&k); err != nil {
		panic(fmt.Sprintf("Bad Kitty %v", err))
	} else {
		fmt.Printf("Good Kitty %#v\n", k)
	}
}

It prints "Good Kitty main.Kitten{MaxHP:0}" , I think Set should return an error in this case

Playground link https://go.dev/play/p/KUi5L1hP-IF

licaonfee avatar Dec 16 '24 22:12 licaonfee

Just ran into this myself. At the very least there should be a configuration option to specify whether errors should be thrown or just let it set the zero value.

ryanulit avatar Aug 14 '25 14:08 ryanulit