dongqing344716
2016-06-12 22:29 阅读 167
已采纳

我如何输出在Docker-Compose.yml的Yaml解组中抛出错误的行?

I'm writing some code that takes in a Docker-Compose.yml and unmarshals it into a struct. I take the docker compose data in as a []byte.

    dockerData []byte
    var struct *struct
    if err := yaml.Unmarshal(dockerData, &struct)

I run a lot of compose files through this process. When errors are thrown, is there a way to output the line or section of the compose file that threw the error?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dsf23223 dsf23223 2016-06-15 03:16

    Set flag on the logger to print out the line number

    // main.go or config file
    
    func init() {
        log.SetFlags(log.LstdFlags | log.Lshortfile)
    }
    

    You can find more of these flags here

    handle the error

    dockerData []byte
    var struct *struct
    if err := yaml.Unmarshal(dockerData, &struct); err != nil {
        log.Print(err)
    }
    
    点赞 评论 复制链接分享

相关推荐