Fork me on GitHub

Golang之旅32-time & error

time包及错误处理

时间和日期

函数都在time包中

  • 如何获取当前的时间:年月日及具体时间等
  • 如何实现时间的格式化

错误处理机制:

  • 通过defer进行处理
  • 通过panicrecover进行处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"fmt"
"time"
)

func main(){
// 获取当前时间
now := time.Now()
fmt.Println(now)

//获取年月日等
fmt.Printf(now.Year())
fmt.Printf(now.Month())
mt.Printf(int(now.Month())) // 转成数字型
fmt.Printf(now.Day())
fmt.Printf(now.Hour())
fmt.Printf(now.Minute())
fmt.Printf(now.Second())

//格式化时间和日期
//1. 使用Printf或者Sprintf
fmt.Printf("当前年月日。%d-%d-%d %d:%d:%d", now.Year(),now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())

datestr := fmt.Sprintf("当前年月日。%d-%d-%d %d:%d:%d", now.Year(),now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())
fmt.Printf("dateStr=%v\n", dateStr)

// 2. time.Format()
fmt.Printf(now.Format("2006/01/02 15:04:05")) // 时间是固定的
fmt.Println()
fmt.Printf(now.Format("2006-01-02"))
fmt.Println()
fmt.Printf(now.Format("15:04:05"))
fmt.Println()
}

时间常量

在程序中获取指定时间的单位:1000 * time.Millisencond

1
2
3
4
5
6
7
8
const(
Nanoseconf Duration // 纳秒
Microsecond // 微秒
Millisencond // 毫秒
Second // 秒
Minute // 分钟
Hour // 小时
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main
import (
"fmt"
"time"
)

func main(){
i := 0
for {
i++
fmt.Println(i)
// 休眠设置
// time.Sleep(time.Second)
time.Sleep(time.Millisecond * 100) // 0.1秒
if i == 100{
break
}
}
}

Unix和UnixNano

两个函数返回的是从1970/01/01 00:00:00到现在的秒或者纳秒

  • Unix
1
func (t Time) Unix() int64
  • UnixNano

    1
    func (t Time) UnixNano() int64
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package main
    import (
    "fmt"
    "time"
    )

    func main(){
    // 获取当前时间
    now := time.Now()
    fmt.Println(now)

    // 获取时间戳
    fmt.Printf("unix时间戳=%v unixnano时间戳=%v\n", now.Unix(), now.UnixNano())
    }

练习题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main
import (
"fmt"
"time"
"strconv"
)

func test(){
str := ""
for i := 0;i < 100000;i++{
str += "hello" + strconv.Itoa(i)
}
}

// 获取上面代码的执行时间
func main(){
start := time.Now().Unix()
test()
end := time.Now().Unix()
fmt.Printf(end - start)
}

内置函数

  • new()
    • 主要是分配值类型的内存:int\float32\struct
    • 返回的是指针
1
2
3
4
5
6
7
8
9
10
11
12
package main
import "fmt"

func main(){
num1 := 100
fmt.Println("num1的类型是%T,num1的值=%v, num1的地址=%v\n", num1, num1, &num1)

// new()
num2 := new(int) // *int
// *num2 = 100 能够改变num的值
fmt.Println("num2的类型是%T,num2的值=%v, num2的地址=%v\n,num2这个指针指向的值=%v\n", num2, num2, &num2, *num2)
}

M9T1ns.png

  • make()
    • 给引用类型分配内存:channel、slice、map

错误处理机制

  • defer
  • panic
  • recover

go中可以抛出一个panic的异常,通过defer的recover捕获这个异常,然后处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main
import (
"fmt"
"time"
)

func test(){
// defer+recover 来捕获和处理异常
defer func(){ // defer语句是最后执行的
// err := recover() // 内置函数;下面的写法也是OK
if err := recover() ; err != nil { // 说明有异常
fmt.Println("err=", err)
// 可以将异常发送给管理员
fmt.Println("发送邮件给123456@qq.com")
}
}() // 匿名函数执行调用
num1 := 10
num2 := 0
res := num1 / num2
fmt.Println("res=", res)
}

func main(){
test()
for {
fmt.Println("main()下面的函数")
time.Sleep(time.Second)
}
}

自定义错误

  • errors.New(“错误说明”),返回一个error类型的值,表示一个错误
  • panic内置函数,接收一个interface()类型的值,输出错误信息,退出程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main
import (
"fmt"
"errors"
)

// 方法1: 通过errors:New进行返回
func readConf(name string)(err error){ // ;传入的参数是文件名;返回的值是err类型
if name == :"config.ini"{ // 如果文件名刚好等于config.ini
// 读取文件正确,返回空值
return nil
}else {
return errors.New("读取文件错误。。。。")
}
}

// 方式2:通过panic机制进行返回
func test(){
err := readConf("config2.ini")
if err != nil{
panic(err) // panic返回
}
fmt.Println("test函数继续执行")
}

func main(){
test()
ftm.Println("main()下面的代码。。。。")
}

本文标题:Golang之旅32-time & error

发布时间:2019年11月06日 - 23:11

原始链接:http://www.renpeter.cn/2019/11/06/Golang%E4%B9%8B%E6%97%8532-time.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea