golang-奇淫巧技

本文介绍了Go语言中动态二维数组的定义与初始化,展示了如何去除字符串中的空格和换行符,以及判断字符是否为字母或数字。此外,还讲解了for循环的注意事项,自定义错误信息的方法,基本类型转换,时间操作如获取当前时间、时间戳转换和获取前n天时间,以及如何生成int64类型的唯一ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


定义与初始化 - 动态二维数组
func main(){
	var arr [][]int
	var tmp []int

	//先创建一维数组,然后再添加给二维数组
	for i:=0; i<5; i++{
		tmp = append(tmp, i)
		arr = append(arr,tmp)
	}
	fmt.Println("arr:",arr)
}

打印:
arr: [[0] [0 1] [0 1 2] [0 1 2 3] [0 1 2 3 4]]


去除字符串空格和换行符

把空格和换行符替换掉…

func main(){
	str := " fkui08 23    "
	str = strings.Replace(str," ","",-1)
	str = strings.Replace(str,"\n","",-1)
	fmt.Printf("new str=%s over\n",str)
}

输出 : new str=fkui0823 over


判断字符是否为 大小写字母,数字
func main(){
	set := []*unicode.RangeTable{unicode.Han, unicode.P}	// 将 set 设置为“汉字、标点符号”
	str := "abT97看Wd"
	for _, v := range str{
		if unicode.IsUpper(v){
			fmt.Printf("is upper=%c\n",v)	// 如果字符是大写,则打印
		}
		if unicode.IsLower(v){
			fmt.Printf("is lower=%c\n",v)	// 如果字符是小写,则打印
		}
		if unicode.IsDigit(v){
			fmt.Printf("is Digit=%c\n",v)	// 如果字符是十进制数字,则打印
		}
		if unicode.IsNumber(v){
			fmt.Printf("is Number=%c\n",v)	// 如果字符是数字字符,则打印
		}
		if unicode.IsOneOf(set, v) {
			fmt.Printf("is chinese=%c\n", v)	// 如果字符是汉字,则打印
		}
	}
}

for index,value := range xxx的注意事项

使用range来遍历数组或者其他可遍历的数据结构时候,value使用的都是同一个内存地址,如果你遍历之后想赋值给某个指针的话,指针指向的值会一直被覆盖,从而导致和预期不符。


自己定义error信息

err.Error()方法可以得到err的定义string

var err error
err = errors.new("not find")
if err.Error() == "not find"{
	.....
}

基本类型之间的转换
  • int转string
string:=strconv.Itoa(int)  

获取时间

具体可以请看这个文章:
https://www.cnblogs.com/shuiche/p/11168216.html
这里讲一下我遇到的问题。

  • 如果需要单独得到月份, 这样写:
month := time.Now().Month()
//这里有个问题,这里得到的month是一个枚举类型,你需要得到确切的月份的表示的话,这样来
switch month {
	case time.January:
		fmt.Println("1月")
	case time.February:
		fmt.Println("2月")
	case time.March:
		fmt.Println("3月")
	case time.April:
		fmt.Println("4月")
	case time.May:
		fmt.Println("5月")
	case time.June:
		fmt.Println("6月")
	case time.July:
		fmt.Println("7月")
	case time.August:
		fmt.Println("8月")
	case time.September:
		fmt.Println("9月")
	case time.October:
		fmt.Println("10月")
	case time.November:
		fmt.Println("11月")
	case time.December:
		fmt.Println("12月")
	}

当然这个是我的笨办法,大佬们要是有更好的办法请指正…


时间格式字符串 to 时间戳
toBeCharge := "2021-01-06 13:22:35"                             //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
timeLayout := "2006-01-02 15:04:05"                             //转化所需模板
theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, time.Local) //使用模板在对应时区转化为time.time类型
sr := theTime.Unix()                                            //转化为时间戳 类型是int64
fmt.Println(theTime)                                            //打印输出theTime 2015-01-01 15:15:00 +0800 CST
fmt.Println(sr)                                                 //打印输出时间戳 1420041600


获取前n天时间
//获取前n天时间
tm := time.Unix(time.Now().Unix(), 0)	//获取当前时间戳
tms := tm.Format("2006-01-02")		//转化为 年-月-日 的格式
fmt.Println(tms)

nowt := time.Now()		//获取当前时间
ptime := nowt.AddDate(0,0,-1)	//根据当前时间获取前一天时间,前2天改成-2即可
pp := ptime.Format("2006-01-02")
fmt.Println(pp)

获取 xx 时间段
lc,_ := time.LoadLocation("Local")	//服务器设置的时区
//获取当前时间,并把结束时间设置为 xxxx年xx月xx日 xx时xx分xx秒
t := time.Now()
endTime := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, lc)
fmt.Println("endTime: ",endTime)
//设置开始时间为前10秒,其他时间以此类推
startTime := endTime.Add(time.Duration(-10)*time.Second)	
fmt.Println("startTime:",startTime)

生成int64类型的唯一id
  1. 生成随机数,
    分布式部署的话这里的workId需要对应各个机器,
    单机用随机数即可,虽然有伪随机数任然有可能重复,但几率不大,这里演示单机的情况.
  2. go get github.com/gitstliu/go-id-worker 下载工具包
package main

import (
	"fmt"
	"github.com/gitstliu/go-id-worker"
	_ "log"
	"math/rand"
	"net"
	"time"
)

func RandInt64(min, max int64) int64 {
	if min >= max || min==0 || max==0{
		return max
	}
	return rand.Int63n(max-min)+min
}

func main() {

	var err error
	var workId int64	//工作机器的id, 这里单机使用随机数
	rand.Seed(time.Now().UnixNano())
	//循环20次看看
	for i:=0;i<20;i++{
		workId = RandInt64(1,1000)	//1-1000的随机数
		fmt.Println("workId:",workId)
		//使用分布式唯一ID - snowflake算法生成唯一ID
		currWoker := &idworker.IdWorker{}
		err = currWoker.InitIdWorker(workId, 1)
		if err!=nil{
			return
		}
		newID, err := currWoker.NextId()
		if err == nil {
			fmt.Println(newID)
		}
	}

}


遇到一个记录一个,持续更新…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值