Go基础积累

func main() {
	//string转int
	str1:="123"
	num1,_:=strconv.Atoi(str1)
	fmt.Println(num1)

	//int转string
	str2:=strconv.Itoa(num1)   //方法一
	str2 = fmt.Sprintf("%d",num1)  //方法二
	fmt.Println(str2)

	//int64转string
	var num2 int64 = 100
	str3:= strconv.FormatInt(num2, 10)
	fmt.Println(str3)

	//string转 float64
	float64Num,_ := strconv.ParseFloat(str1,64)

    //string转 int64
	int64Num, _ := strconv.ParseInt(str1, 10, 64)
	fmt.Println(float64Num, int64Num)

	//时间转字符串
	now:=time.Now()
	nowStr:=now.Format("2006-01-02")
	fmt.Println(nowStr)

	//字符串转时间
	nowStr = "2021-01-01"
	now,_ = time.Parse("2006-1-2",nowStr)
	
	//构造特定时间
	year, month, day := now.Date()
	tomorrow := time.Date(year, month, day+1, 8, 0, 0, 0, now.Location())
	fmt.Println(tomorrow)
	
	//加两个半小时
	tomorrow = tomorrow.Add(time.Hour * 2).Add(time.Minute * 30)
	
	//获取两个时间之间相差的秒数
	seconds := tomorrow.Sub(now).Seconds()
	fmt.Println(int(seconds))
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值