go-toml元帅失败

ndasle7k  于 5个月前  发布在  Go
关注(0)|答案(1)|浏览(56)

下面的程序试图封送一个结构并写入文件。该结构是嵌套的,它包含toml标记。我没有收到任何错误消息,它似乎是正确的。结构的打印是好的。程序的输出如下:

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

字符串
创建的文件为空:

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
}


有什么好理由吗?

bvk5enib

bvk5enib1#

您应该导出结构体的字段:

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
}

字符串
输出将是:

[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]}]}


参见https://go.dev/play/p/49MrxpqZ8p5

相关问题