杜松子酒输出到文件

I'm using Gin to make REST API server and Gin shows its output on a console like this: Gin console example

I'd like to make gin's output to a file instead of console.

What I've found is below code from Gin/mode.go

// DefaultWriter is the default io.Writer used the Gin for debug output and
// middleware output like Logger() or Recovery().
// Note that both Logger and Recovery provides custom ways to configure their
// output io.Writer.
// To support coloring in Windows use:
//      import "github.com/mattn/go-colorable"
//      gin.DefaultWriter = colorable.NewColorableStdout()
var DefaultWriter io.Writer = os.Stdout
var DefaultErrorWriter io.Writer = os.Stderr

Looks like I can change DefaultWriter and DefaultErrorWriter's behavior by setting like

gin.DefaultWriter = something

on my code.

But I have no idea how to write that 'something' code; it must be a function which writes to a file but have no idea how/where to start.

So, my questions are:

  1. Am I heading to the right direction?
  2. How to write that 'something' function? It would be very helpful if you can provide an example.

Thank you.

You can use os package to create a file.

file, fileErr := os.Create("file")
if fileErr != nil {
    fmt.Println(fileErr)
    return
}
gin.DefaultWriter = file

This should create a file and start writing to it.