Skip to content

toml.Unmarshal returning empty slice when using struct #962

Answered by pelletier
cyrus01337 asked this question in Questions
Discussion options

You must be logged in to vote

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

package main

import (
	"fmt"

	"github.com/pelletier/go-toml/v2"
)

type MappedConfiguration struct {
	DotfilesRepository string   `toml:"dotfiles_repository"`
	ProjectPatterns    []string `toml:"project_patterns"`
}

func main() {
	var unmappedConfiguration MappedConfiguration
	doc := `
        dotfiles_repository = "https://github.com/cyrus01337/dotfiles"
        project_patterns = ["$HOME/Projects/*/*", "$HOME/Playg…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@cyrus01337
Comment options

Answer selected by cyrus01337
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants