toml.Unmarshal returning empty slice when using struct #962
-
I have the below code that represents parsing a TOML file's contents. For some reason this doesn't seem to work as shown in the examples, what am I missing exactly? type UnmappedConfiguration struct {
dotfiles_repository string
project_patterns []string
}
type MappedConfiguration struct {
DotfilesRepository string
ProjectPatterns []string
}
func parseConfiguration() (configurationPointer *Configuration, exception error) {
var unmappedConfiguration UnmappedConfiguration
// emulates os.ReadFile post error-handling
doc := `
dotfiles_repository = "https://github.com/cyrus01337/dotfiles"
project_patterns = ["$HOME/Projects/*/*", "$HOME/Playground/", "$ZDOTDIR/"]
test = "test"
`
exception = toml.Unmarshal([]byte(doc), &unmappedConfiguration)
if exception != nil {
return nil, exception
}
fmt.Println(unmappedConfiguration) // { []}
return &configurationPointer, nil
} I have seen #873 though as I'm new to this library, I may be mistaken in thinking this isn't related to the library itself and is purely a user issue. Do correct me if I'm wrong however as I may not know better in this situation. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi! go-toml only unmarshals public fields of structs (same behavior as encoding/json). You can use the https://go.dev/play/p/LoLPP1BZygY
|
Beta Was this translation helpful? Give feedback.
Hi! go-toml only unmarshals public fields of structs (same behavior as encoding/json). You can use the
toml
tag to specify the name of the toml key if it differs from the fields' name. For example, I think the following does what you want:https://go.dev/play/p/LoLPP1BZygY