dongqing344716 2016-06-12 22:29
浏览 198
已采纳

我如何输出在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 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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?