How to write log into .log files in golang?
<p>I'am new use go language, still learn and need help. How can I write my application logs in go language into ".log" files similiar like PHP phalcon framework ?</p>

<p>I have searching google and get go language tutorials, but there is no simple understanding example. I have try some example but the logs text doesn't write continously. Here's the example that I used to learn for logging log, and I think it's completly wrong.</p>

<pre><code>package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "os"
    "time"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}



func main() {

    now := time.Now()
    dt := now.Format("2006-01-02")

    dt2 := now.Format("2006-01-02 15:04:05")

    // To start, here's how to dump a string (or just
    // bytes) into a file.
    d1 := []byte("hello\ngo11\n" + dt2)
    err := ioutil.WriteFile("/Users/my/Documents/work/src/logs/log-"+dt+".log", d1, 0644)
    check(err)

    // For more granular writes, open a file for writing.
    f, err := os.Create("/Users/my/Documents/work/src/logs/log1.log")
    check(err)

    // It's idiomatic to defer a `Close` immediately
    // after opening a file.
    defer f.Close()

    // You can `Write` byte slices as you'd expect.
    d2 := []byte{115, 111, 109, 101, 10}
    n2, err := f.Write(d2)
    check(err)
    fmt.Printf("wrote %d bytes\n", n2)

    // A `WriteString` is also available.
    n3, err := f.WriteString("writes\n" + dt)
    fmt.Printf("wrote %d bytes\n", n3)

    // Issue a `Sync` to flush writes to stable storage.
    f.Sync()

    // `bufio` provides buffered writers in addition
    // to the buffered readers we saw earlier.
    w := bufio.NewWriter(f)
    n4, err := w.WriteString("buffered\n")
    fmt.Printf("wrote %d bytes\n", n4)

    // Use `Flush` to ensure all buffered operations have
    // been applied to the underlying writer.
    w.Flush()

}
</code></pre>