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

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

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

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

  1. stream-gen git:(main) go run main.go
  2. startTime:
  3. stream-gen git:(main) go run main.go
  4. startTime:
  5. stream-gen git:(main) go run main.go
  6. startTime:
  7. stream-gen git:(main) go run main.go
  8. startTime:
  9. stream-gen git:(main) go run main.go
  10. startTime:
  11. stream-gen git:(main) go run main.go
  12. startTime:
  13. stream-gen git:(main) go run main.go
  14. startTime: 2022-01-01T00:00:00Z
  15. stream-gen git:(main) go run main.go
  16. startTime:
  17. stream-gen git:(main) go run main.go
  18. startTime:
  19. stream-gen git:(main) go run main.go
  20. startTime: 2022-01-01T00:00:00Z
  21. stream-gen git:(main) go run main.go
  22. 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:

  1. stream-gen git:(main) go run main.go
  2. startTime:
  3. stream-gen git:(main) go run main.go
  4. startTime:
  5. stream-gen git:(main) go run main.go
  6. startTime:
  7. stream-gen git:(main) go run main.go
  8. startTime:
  9. stream-gen git:(main) go run main.go
  10. startTime:
  11. stream-gen git:(main) go run main.go
  12. startTime:
  13. stream-gen git:(main) go run main.go
  14. startTime: 2022-01-01T00:00:00Z
  15. stream-gen git:(main) go run main.go
  16. startTime:
  17. stream-gen git:(main) go run main.go
  18. startTime:
  19. stream-gen git:(main) go run main.go
  20. startTime: 2022-01-01T00:00:00Z
  21. stream-gen git:(main) go run main.go
  22. 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:

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

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

  1. type ConfigNew struct {
  2. Interval string `toml:"interval"`
  3. MetricBatchSize string `toml:"metric-batch-size"`
  4. StartTime time.Time `toml:"start-time"`
  5. EndTime time.Time `toml:"end-time"`
  6. Format string `toml:"format"`
  7. Influxdb struct {
  8. Urls []string `toml:"urls"`
  9. Database string `toml:"database"`
  10. RetentionPolicy string `toml:"retention-policy"`
  11. Timeout string `toml:"timeout"`
  12. } `toml:"influxdb"`
  13. }

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:

确定