自己写的golang常用函数,正则验证,过滤词,时间戳

package controller

import (
	"regexp"
	"strconv"
	"strings"
	"time"
)
//获取int型时间戳
func GetIntTimestamp() int {
	strInt64 := time.Now().Unix()
	id16,_ := strconv.Atoi(strconv.FormatInt(strInt64,10))
	return id16
}
//获取string型时间戳
func GetSringTimestamp() string {
	strInt64 := time.Now().Unix()
	id16:= strconv.FormatInt(strInt64,10)
	return id16
}
//获取Y-m-d H:i:s格式的日期
func GetCurrentDate() string {
	currentTiem := time.Now()
	currentData := currentTiem.Format("2006-01-02 15:04:05")
	return currentData
}
//过滤敏感词
func IsWordShield(word string) bool  {
	wordList := []string{
		"测试",
	}
	for i:=0;i<len(wordList);i++{
		if(strings.Index(word,wordList[i]) !=-1){
			return false
		}
	}
	return  true
}
//验证手机号
func VerfyPhone(phone string) bool  {
	regStr := `^1[3456789]\d{9}$`
	if match, _ := regexp.MatchString(regStr, phone); match {
		return true
	}
	return false
}
//存储错误日志
func LogEorror(e interface{}) {
	file := "./" + time.Now().Format("2006-01-02") + "_log" + ".txt"
	logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
	if err != nil {
		log.Fatal(err)
	}
	defer logFile.Close()
	loger = log.New(logFile, "ERROR:", log.Ltime|log.Lshortfile) // 将文件设置为loger作为输出
	loger.Print(e)
}
//postJson请求
func httpPostJson(url string, json string, header map[string]string) {
	jsonStr := []byte(json)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	if err != nil {
		fmt.Println(err)
	}
	for k, v := range header {
		req.Header.Set(k, v)
	}
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		// handle error
		fmt.Println(err)
		return
	}
	defer resp.Body.Close()

	statuscode := resp.StatusCode
	hea := resp.Header
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
	fmt.Println(statuscode)
	fmt.Println(hea)

}
//md5
func stringToMd5(aa string){

//将key的md5作为id
		h := md5.New()
		h.Write([]byte(aa))
		md5String = hex.EncodeToString(h.Sum(nil))
		fmt.Println(md5String )
}
//加载yaml配置文件
func loadYaml() (yamlConfig Config) {
	config := Config{}
	yamlS, _ := ioutil.ReadFile("./config.yaml")
	err := yaml.Unmarshal(yamlS, &config)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(config)
	return config
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值