main
wpl 2024-10-21 15:54:36 +08:00
parent 083ec1e2f4
commit 38a65d5d2d
3 changed files with 52 additions and 11 deletions

20
day06/json_demo/main.go Normal file
View File

@ -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)
}

View File

@ -1,8 +1,11 @@
package mylogger_test
import "fmt"
import (
"fmt"
"time"
)
//logger 日志结构体
// logger 日志结构体
type Logger struct {
Level LogLevel
}
@ -10,18 +13,25 @@ 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)

View File

@ -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
}