下面的程序试图封送一个结构并写入文件。该结构是嵌套的,它包含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
}
型
有什么好理由吗?
1条答案
按热度按时间bvk5enib1#
您应该导出结构体的字段:
字符串
输出将是:
型
参见https://go.dev/play/p/49MrxpqZ8p5