首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
fmt.Printf("【%+d,%+d】", 0, -0)
|
||||
|
||||
userID := 1001
|
||||
action := "登录"
|
||||
|
||||
// 1. fmt.Printf:控制台输出结构化日志
|
||||
fmt.Printf("[%s] 用户%d执行%s操作\n",
|
||||
time.Now().Format("15:04:05"), userID, action)
|
||||
|
||||
// 2. fmt.Sprintf:生成审计日志字符串
|
||||
auditLog := fmt.Sprintf("audit|%d|%s|%s",
|
||||
userID, time.Now().Format("2006-01-02"), action)
|
||||
fmt.Println("生成审计日志:", auditLog)
|
||||
|
||||
// 3. fmt.Fprintf:写入文件(处理错误)
|
||||
file, err := os.Create("operation.log")
|
||||
if err != nil {
|
||||
fmt.Printf("文件创建失败:%v\n", err)
|
||||
return
|
||||
}
|
||||
defer file.Close() // 确保文件关闭
|
||||
fmt.Fprintf(file, "%s - 用户%d %s\n", time.Now(), userID, action)
|
||||
|
||||
// 4. fmt.Fprintf:写入内存缓冲区
|
||||
var buf bytes.Buffer
|
||||
fmt.Fprintf(&buf, "缓存记录:用户%d的%s操作(时间:%s)",
|
||||
userID, action, time.Now().Format("15:04:05"))
|
||||
fmt.Println("缓冲区内容:", buf.String())
|
||||
}
|
||||
Reference in New Issue
Block a user