diff --git a/day06/json_demo/main.go b/day06/json_demo/main.go new file mode 100644 index 0000000..ed5b768 --- /dev/null +++ b/day06/json_demo/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "encoding/json" + "fmt" +) + +//json + +type person struct { + Name string `json:"name"` + Age int `json:"age"` +} + +func main() { + str := `{"name":"周林","age":9000}` + var p person + json.Unmarshal([]byte(str), &p) //注意这里传值要对应传递结构体类型,根据结构体的key去对应匹配value + fmt.Println(p.Name, p.Age) +} diff --git a/day06/mylogger_test/console.go b/day06/mylogger_test/console.go index 03b955b..4b68619 100644 --- a/day06/mylogger_test/console.go +++ b/day06/mylogger_test/console.go @@ -1,8 +1,11 @@ package mylogger_test -import "fmt" +import ( + "fmt" + "time" +) -//logger 日志结构体 +// logger 日志结构体 type Logger struct { Level LogLevel } @@ -10,21 +13,28 @@ type Logger struct { //Newlog 构造函数 func NewLog(levelStr string) Logger { - level,err :=parseLogLevel(levelStr) - if err !=nil{ + level, err := parseLogLevel(levelStr) + if err != nil { panic(err) } return Logger{ - Level:level, + Level: level, } } +func log(lv logLevel,msg string){ + fmt.Printf() +} + func (l Logger) Debug(msg string) { - now :=time.Now() - fmt.Printf("[%s] %s" now.Format("2006-01-02 15:04:05"),msg) + if l.enable(DEBUG) { + now := time.Now() + funcName, fileName, lineNo := getInfo(2) + fmt.Printf("[%s][DEBUG][%s:%s:%d]%s", now.Format("2006-01-02 15:04:05"), ,fileName,funcName,lineNumber,msg) + } } func (l Logger) Info(msg string) { - fmt.Println(msg) + fmt.Println(msg) } func (l Logger) Warning(msg string) { fmt.Println(msg) diff --git a/day06/mylogger_test/mylogger.go b/day06/mylogger_test/mylogger.go index c227bed..22d699c 100644 --- a/day06/mylogger_test/mylogger.go +++ b/day06/mylogger_test/mylogger.go @@ -1,11 +1,22 @@ package mylogger_test -import "runtime" +import ( + "fmt" + "path" + "runtime" +) func main() { } -func getInfo(n int) { - pc, fine, line, ok := runtime.Caller() +func getInfo(skip int) (funcName, fileName string, lineNo int) { + pc, file, lineNo, ok := runtime.Caller(skip) + if !ok { + fmt.Printf("runtime.Caller() failed\n") + return + } + funcName = runtime.FunForPC(pc).Name() + fileName = path.Base(file) + return }