Go从0开发(7)_时间/文件/日志/反射操作

time操作 字符转时间  时间格式化

package main

import (
"fmt"
"time"
)

func time1() {
now := time.Now()
fmt.Println(now)
fmt.Println(now.Year())
fmt.Println(now.Month())
fmt.Println(now.Day())
fmt.Println(now.Date())
fmt.Println(now.Hour())
fmt.Println(now.Minute())
fmt.Println(now.Second())
fmt.Println()

//时间戳
fmt.Println(now.Unix())
fmt.Println(now.UnixNano())
fmt.Println()

//时间间隔
fmt.Println(time.Second*20)
//now + 24
fmt.Println(now.Add(24*time.Hour))

//定时器
timer := time.Tick(time.Second)
for t:= range timer{
fmt.Println("定时器",t)
}

}

//时间格式化
func time2() {

now := time.Now()
fmt.Println(now.Format("2006-01-02"))
fmt.Println(now.Format("2006-01-02 15:04:05")) //24小时
fmt.Println(now.Format("2006-01-02 03:04:05")) //12小时
fmt.Println(now.Format(".000")) //12小时
}

//字符转时间
func time3() {
now, _ := time.Parse("2006-01-02 15:04:05", "2020-07-03 21:13:56")
fmt.Println(time.Now().Unix())
fmt.Println(now.Unix())
}

//时区
func time4() {
now := time.Now()
fmt.Println(now)

t1,_ :=time.Parse("2006-01-02 15:04:05","2020-07-03 21:35:36")
fmt.Println(t1)
loc,_:=time.LoadLocation("Asia/Shanghai")
t2,_ := time.ParseInLocation("2006-01-02 15:04:05","2020-07-03 21:35:36",loc)
fmt.Println(t2)

//时间相减 t2-t1=t3
t3 := t2.Sub(t1)
fmt.Println(t3)
}

func main() {
time4()
}

获取文件信息  文件位置  函数  获取当前行 

//获取文件信息
func getfileinfo(num int) (FuncName, FileName string, LineNo int) {
pc, file, line, _ := runtime.Caller(num)
FuncName = runtime.FuncForPC(pc).Name() //获取函数名字
FileName = path.Base(file) //获取当前文件名字
LineNo = line //获取当前行号
return
}

直接获取文件信息/修改文件名

func main()  {
//获取文件信息
fileobj,_ := os.Open(`E:\Go\src\DarkHorse\studygo\day06\日志.log`)
aa,_:=fileobj.Stat()
fmt.Println(aa.Size())
fmt.Println(aa.Name())
fmt.Println(aa.Sys())
fmt.Println(aa.ModTime())
fmt.Println(aa.Mode())
fmt.Println(aa.IsDir())

//改文件名字
os.Rename(`E:\Go\src\DarkHorse\studygo\day06\555.log`,`E:\Go\src\DarkHorse\studygo\day06\798.log`)
}

日志库

package main

import (
"github.com/labstack/gommon/log"
"os"
"time"
)

func log1() {
file,_ := os.OpenFile(`E:\Go\src\DarkHorse\studygo\day06\日志.log`,os.O_APPEND|os.O_CREATE|os.O_WRONLY,0664)
log.SetOutput(file)
for {
log.Error("错误输出")
log.Debug("调试")
log.Info("Info")
log.Warn("警告")
log.Fatal(" 致命的 ")
time.Sleep(time.Second)
}
}

func main() {
log1()
}

反射


import (
"encoding/json"
"fmt"
"reflect"
)

type studes struct {
Name string `json:"name"`
Age int `json:"age"`
}

func re() {
str := `{"name":"zhangsan","Age":16}`
var s studes
json.Unmarshal([]byte(str), &s)
fmt.Println(s)
}

//获取变量类型和值
func reflectValue(x interface{}) {
v := reflect.ValueOf(x) //获取变量值
k := v.Kind() //变量类型

switch k {
case reflect.Int64:
fmt.Printf("type is Int64,value is %d \n", int64(v.Int()))
case reflect.Float32:
fmt.Printf("type is Int64,value is %f \n", float32(v.Float()))
case reflect.Float64:
fmt.Printf("type is Int64,value is %f \n", float64(v.Float()))
}
}
func f1() {
var a = 12.36
reflectValue(a)
}

func reflectSetValue1(x interface{}) {
v := reflect.ValueOf(x) //获取变量值
k := v.Elem().Kind() //变量类型
fmt.Println(k)
if k == reflect.Int64 {
v.Elem().SetInt(200)
}
}
func f2() {
var a int64 = 12
reflectSetValue1(&a)
fmt.Println(a)
}

func f3() {
var a11 int
a := reflect.ValueOf(&a11).IsNil() //判断指针是否为空
fmt.Println(a)
fmt.Println(reflect.ValueOf(nil).IsValid()) //判断返回值是否有效
}
func isfunc(s string) bool {
return true
}

func f4() {
a := studes{}
fmt.Println(reflect.ValueOf(a).FieldByName("Name").IsValid()) //判断该成员 Name 是否是 studes 的成员
fmt.Println(reflect.ValueOf(a).FieldByNameFunc(isfunc).IsValid()) //判断改方法 Name 是否是 studes 的成员
}

func main() {
f4()
}



伍先生
  • 职业: 程序员,产品
  • 码龄: 4.1
  • 技能: PHP Go 前端
  • 微信: JwCode
  • 公众号/小程序: 渐悟分享