解析 toml 文件并不总是产生预期的结果。

huangapple go评论83阅读模式
英文:

Parsing toml files does not always produce the expected results

问题

这是源代码:https://github.com/ivankf/stream-gen/blob/main/main.go#L1:L55

这是代码中的toml文件:https://github.com/ivankf/stream-gen/blob/main/etc/sample.conf#L1:L21

我运行了十多次,只有两次返回了正确的结果:

➜  stream-gen git:(main) ✗ go run main.go       
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 2022-01-01T00:00:00Z
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 2022-01-01T00:00:00Z
➜  stream-gen git:(main) ✗ go run main.go
startTime: 

环境信息:

Golang版本:go1.18.3 darwin/arm64

github.com/BurntSushi/toml v1.2.0

英文:

This is source code: https://github.com/ivankf/stream-gen/blob/main/main.go#L1:L55

This is the toml file in the code: https://github.com/ivankf/stream-gen/blob/main/etc/sample.conf#L1:L21

I ran it more than ten times, and only two times returned the correct results:

➜  stream-gen git:(main) ✗ go run main.go       
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 2022-01-01T00:00:00Z
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 
➜  stream-gen git:(main) ✗ go run main.go
startTime: 2022-01-01T00:00:00Z
➜  stream-gen git:(main) ✗ go run main.go
startTime: 

environmental information:

Golang Version: go1.18.3 darwin/arm64

github.com/BurntSushi/toml v1.2.0

答案1

得分: 1

你的代码出现了以下错误:

(last key "influxdb.timeout"): incompatible types: TOML value has type string; destination has type integer
startTime:

我尝试使用以下结构体来解决你的Toml文件问题,并且没有出现任何问题。

type ConfigNew struct {
Interval string toml:"interval"
MetricBatchSize string toml:"metric-batch-size"
StartTime time.Time toml:"start-time"
EndTime time.Time toml:"end-time"
Format string toml:"format"
Influxdb struct {
Urls []string toml:"urls"
Database string toml:"database"
RetentionPolicy string toml:"retention-policy"
Timeout string toml:"timeout"
} toml:"influxdb"
}

我使用了以下的Toml转结构体生成器来生成这个结构体。

https://xuri.me/toml-to-go/

英文:

You are getting following error for your code:

    (last key "influxdb.timeout"): incompatible types: TOML value has type string; destination has type integer
startTime: 

I tried with following struct for your Toml file and it worked without an issue.

type ConfigNew struct {
	Interval        string    `toml:"interval"`
	MetricBatchSize string    `toml:"metric-batch-size"`
	StartTime       time.Time `toml:"start-time"`
	EndTime         time.Time `toml:"end-time"`
	Format          string    `toml:"format"`
	Influxdb        struct {
		Urls            []string `toml:"urls"`
		Database        string   `toml:"database"`
		RetentionPolicy string   `toml:"retention-policy"`
		Timeout         string   `toml:"timeout"`
	} `toml:"influxdb"`
}

I used following Toml to go struct generator to generate the struct.

https://xuri.me/toml-to-go/

huangapple
  • 本文由 发表于 2022年8月7日 02:22:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73262299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定