-2

I have the following working code

    serverFile, _ := os.OpenFile("server.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    debugFile, _ := os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

    Logger = &BufferedLogger{
        ServerWriter:    serverFile,
        DebugWriter:     debugFile,
        BufferSize:      100,
    }

which I like to simplify, if possible. I tried

    Logger = &BufferedLogger{
        ServerWriter, _:    os.OpenFile("server.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644),
        DebugWriter, _:     os.OpenFile("debug.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644),
        BufferSize:      100,
    }

That is wrong syntax. Can someone give me a hint to fix it, or is that impossible?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Stefan
  • 1,789
  • 1
  • 11
  • 16

1 Answers1

3

No, you can't do it all in one statement. And there's a reason for that: you should be handling the error, not ignoring it. Any function that might cause an error will have a multi-valued return, so that you can't use it as an argument to another function, or in an initializer — only in a multi-valued assignment, where you can capture and check the error.

hobbs
  • 223,387
  • 19
  • 210
  • 288