Golang | 常用包整合

MrSnake
2022-01-28 / 2 评论 / 113 阅读 / 正在检测是否收录...

reflect包

type t struct {
    name string
}
reflect.Typeof(t)    // main.t
reflect.Kind(t)    // struct

Typeof指变量所属的类型,Kind指变量所属的类别。

const (Invalid Kind = iota,Bool,Int,Int8,Int16,Int32,Int64,Uint,Uint8,Uint16,Uint32,Uint64,Uintptr,Float32,Float64,Complex64,Complex128,Array,Chan,Func,Interface,Map,Ptr,Slice,String,Struct,UnsafePointer)

2.修改接口实际变量的值

Elem()获取指向原变量的指针

reflect.ValueOf(a).Elem().SetInt(38)

时间转换

1.time.Time to string

time.now().Format("2006-01-02")

2.string to time.Time

todayZero, _ := time.ParseInLocation("2006-01-02", "2021-11-26 15:22:22", time.Local)

3.获取 time.Time 中的年月日

year := time.Now().Year()
month := time.Now().month()
day := time.Now().Day()
// 第二种方式
year, month, day := time.Now().Date()

Tips: month 是 string,year 和 day 是 int

4.int64 转 time(时间戳转为 Time 类型)

timestamp = 1638772947
start := time.Unix(timestamp, 0)

5.判断时间是否为初始值

0001-01-01 00:00:00 +0000 UTC

start, _ := time.Parse(time.RFC850, "")

if start.IsZero() {
    fmt.Println(start)
}

// 输出
// 0001-01-01 00:00:00 +0000 UTC

6.UTC 和 GMT 和 CST

UTC 一般指协调世界时。 协调世界时,又称世界统一时间;

GMT(Greenwich Mean Time),格林尼治标准时间;

CST(China Standard Time),中国标准时间

在不需要精确到秒的情况下,通常将GMT 和UTC 视作等同。但UTC 更加科学更加精确。

CST = UTC + 8 小时

strconv

1

评论 (2)

取消
  1. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  2. 头像
    1
    Windows 10 · Google Chrome

    555

    回复