⚡ 编程实验室🏗️ HTML🎨 CSS⚡ JavaScript🐍 Python🗄️ SQL☕ Java⚛️ React💚 Vue🟢 Node.js⚙️ C语言🐘 PHP🐹 Go🔷 TypeScript🐬 MySQL🔧 C++🎯 C#🦀 Rust🅱️ Bootstrap💡 jQuery🎸 Django🍃 MongoDB👗 Sass🎪 Kotlin📊 R语言📋 XML📊 Excel🐘 PostgreSQL🐳 Docker🅰️ Angular🎮 游戏🏠 网站首页

Go 时间处理 — time 包

时间格式化、计算时间差、定时器、时区处理 · 难度:入门 · +15XP

time 包 — Go 时间处理

Go 的 time 包设计独特——用特定时间作为格式化模板2006-01-02 15:04:05

核心操作

now := time.Now()                          // 当前时间
t := time.Date(2024, 6, 1, 12, 0, 0, 0, time.UTC)

// 格式化(记住模板!) now.Format("2006-01-02 15:04:05") // 2024-06-01 12:00:00 now.Format("2006年01月02日 15:04") // 中文格式

// 时间计算 future := now.Add(24 * time.Hour) // 24小时后 diff := future.Sub(now) // 时间差

// 定时器 timer := time.NewTimer(2 * time.Second) <-timer.C // 等待 2 秒 ticker := time.NewTicker(1 * time.Second) // 每秒触发

Ctrl+Enter
🚀 升级VIP
解锁全部课程+AI助手

🏆 学习排行

加载中...

📊 统计

📖 142 篇
0 完成
🔥 0