我不能从下面的golang代码中获得生命,为什么当你在底部调用write函数时,
func write(message string) {
log.Printf("%v\n", message)
}
为什么 log.Printf 调用下面的方法
func (fl fileLog) Write(data []byte) (int, error ) {
fmt.Println("does this ever get called?")
f, err := os.OpenFile(string(fl), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return 0, err
}
defer f.Close()
return f.Write(data)
}
Logger 的 printf 定义如下
func (*Logger) Printf ¶
func (l *Logger) Printf(format string, v ...interface{})
-- 下面是完整代码,请有人向我解释一下为什么会这样--
package main
import (
"fmt"
stlog "log"
"os"
)
var log *stlog.Logger
type fileLog string
func (fl fileLog) Write(data []byte) (int, error ) {
fmt.Println("does this ever get called?")
f, err := os.OpenFile(string(fl), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return 0, err
}
defer f.Close()
return f.Write(data)
}
func registerHandlers() {
var msgRaw = "how are you"
write(msgRaw)
}
func run(destination string) {
log = stlog.New(fileLog(destination), "", stlog.LstdFlags)
}
func main() {
fmt.Println("here we go")
run("../test.log")
registerHandlers()
}
func write(message string) {
log.Printf("%v\n", message)
}
慕容708150
相关分类