go-toml Marshal fails

45 Views Asked by At

the following program tries to Marshal a struct and write to file. The struct is nested and it contains toml tags. I am not receiving any error message and it seems correct. The print of the structure is ok. The output of the program is the following:

[]
{testServer {/var/www} {/tmp} [{testPolicy [10 20 30] [2 2 3]}]}

The created file is empty:

package main

import (
    "fmt"
    "io/ioutil"
    "log"

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

type source struct {
    path string `toml:"path"`
}

type target struct {
    path string `toml:"path"`
}

type policy struct {
    policyName   string `toml:"policyName"`
    schemaDays   []int  `toml:"schemaDays"`
    schemaCounts []int  `toml:"schemaCounts"`
}

type servers struct {
    serverName string   `toml:"serverName"`
    source     source   `toml:"source"`
    target     target   `toml:"target"`
    policies   []policy `toml:"policies"`
}

func main() {

    var s servers = createConfiguration()
    b, err := toml.Marshal(s)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(b)
    err = ioutil.WriteFile("config.toml", b, 0644)
    if err != nil {
        panic(err)
    }
    fmt.Println(s)

}

func createConfiguration() (server servers) {
    s := source{
        path: "/var/www",
    }
    t := target{
        path: "/tmp",
    }
    d := []int{10, 20, 30}
    c := []int{2, 2, 3}
    p := &policy{
        policyName:   "testPolicy",
        schemaDays:   d,
        schemaCounts: c,
    }
    server = servers{
        serverName: "testServer",
        source:     s,
        target:     t,
        policies:   []policy{*p},
    }
    return
}

Any good reason?

1

There are 1 best solutions below

0
On

You should export the field of the struct:

package main

import (
    "fmt"
    "log"

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

type source struct {
    path string `toml:"path"`
}

type target struct {
    path string `toml:"path"`
}

type policy struct {
    PolicyName   string `toml:"policyName"`
    SchemaDays   []int  `toml:"schemaDays"`
    SchemaCounts []int  `toml:"schemaCounts"`
}

type servers struct {
    ServerName string   `toml:"serverName"`
    Source     source   `toml:"source"`
    Target     target   `toml:"target"`
    Policies   []policy `toml:"policies"`
}

func main() {

    var s servers = createConfiguration()
    b, err := toml.Marshal(s)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(b)
    //  err = ioutil.WriteFile("config.toml", b, 0644)
    if err != nil {
        panic(err)
    }
    fmt.Println(s)

}

func createConfiguration() (server servers) {
    s := source{
        path: "/var/www",
    }
    t := target{
        path: "/tmp",
    }
    d := []int{10, 20, 30}
    c := []int{2, 2, 3}
    p := &policy{
        PolicyName:   "testPolicy",
        SchemaDays:   d,
        SchemaCounts: c,
    }
    server = servers{
        ServerName: "testServer",
        Source:     s,
        Target:     t,
        Policies:   []policy{*p},
    }
    return
}

The output will be:

[115 101 114 118 101 114 78 97 109 101 32 61 32 39 116 101 115 116 83 101 114 118 101 114 39 10 10 91 115 111 117 114 99 101 93 10 10 91 116 97 114 103 101 116 93 10 10 91 91 112 111 108 105 99 105 101 115 93 93 10 112 111 108 105 99 121 78 97 109 101 32 61 32 39 116 101 115 116 80 111 108 105 99 121 39 10 115 99 104 101 109 97 68 97 121 115 32 61 32 91 49 48 44 32 50 48 44 32 51 48 93 10 115 99 104 101 109 97 67 111 117 110 116 115 32 61 32 91 50 44 32 50 44 32 51 93 10]
{testServer {/var/www} {/tmp} [{testPolicy [10 20 30] [2 2 3]}]}

See https://go.dev/play/p/49MrxpqZ8p5