package main

import (
“fmt”
“log”
“time”
)

func main(){
Start()
Search()
}

//全局参数定义,注意只能在本package包里面使用
var TokenExpire map[string]string

func Start() {

TokenExpire = make(map[string]string)
log.Println("开始",time.Now())
//刚开始获取token
Token := SearchToken()
TokenExpire["token"] = Token
//获取token时间
NowTime := time.Now().Format("2006-01-02 15:04:05")
TokenExpire["time"] = NowTime

}

func SearchToken()(string) {

//获取token,下面是测试token
token :="test"
return token

}

func Search() {

now := time.Now()
loc, _ := time.LoadLocation("Asia/Shanghai")
data_time, timeErr := time.ParseInLocation("2006-01-02 15:04:05", TokenExpire["time"], loc)
if timeErr != nil {
	fmt.Println("time_err",timeErr)
}
sub := int(now.Sub(data_time).Hours())
threshold := 24
if sub >=threshold{
	//全局参数里面的token超过24小时,重新获取token
	NowTime := time.Now().Format("2006-01-02 15:04:05")
	TokenExpire["time"] = NowTime
	Token :=SearchToken()
	TokenExpire["token"] = Token
	log.Println("超过",time.Now())

}else {
	log.Println("不超过",time.Now())
}
//下面是可以用token请求接口

}