英文:
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转结构体生成器来生成这个结构体。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论