当前位置:网站首页>golang time strings常用方法
golang time strings常用方法
2022-04-22 10:08:00 【昵称你猜8】
time.Sleep
go用来指定睡眠时间的函数为time.Sleep。传入的为一个Duration,所以如果想睡眠5s钟,不能直接写 time.Sleep(5) ,而应该写time.Sleep(5 * time.Second)
其中time.Second就是一个Duration类型,表示1s的时间间隔,乘系数5就得到5s的时间间隔。
想要睡眠的时间可以使用自由组合,比如睡眠1小时10分5秒:
time.Sleep(1*time.Hour + 10*time.Minute + 5*time.Second)
time.Sleep(time.Duration(5)*time.Second)
使用time.Since计算执行时间
想要获取计算某个任务从开始到结束的运行时间,我们可以使用time包中的Slice函数
package main
import (
"fmt"
"time"
)
// 指定字段类型
func main() {
start := time.Now() // 获取当前时间
sum := 0
for i := 0; i < 100000000; i++ {
sum++
}
elapsed := time.Since(start).Seconds()
fmt.Println(elapsed)
}
strings常用方法
package main
import s "strings" //strings取个别名
import "fmt"
//我们给 fmt.Println 一个短名字的别名,我们随后将会经常用到。
var p = fmt.Println
func main() {
//这是一些 strings 中的函数例子。注意他们都是包中的函数,不是字符串对象自身的方法,这意味着我们需要考虑在调用时传递字符作为第一个参数进行传递。
p("Contains: ", s.Contains("test", "es")) //是否包含:true
p("Count: ", s.Count("test", "t")) //包含字符数 :2
p("HasPrefix: ", s.HasPrefix("test", "te")) //是否包含前缀:true
p("HasSuffix: ", s.HasSuffix("test", "st")) //是否包含后缀:true
p("Index: ", s.Index("test", "d")) //字符存在的位置,不存在返回-1
p("Join: ", s.Join([]string{"a", "b"}, "-")) //拼接字符:a-b
p("Repeat: ", s.Repeat("abc", 5)) //复制字符几次:aaaaa
p("Replace: ", s.Replace("foo", "o", "0", -1)) //替换所有存在的字符 f00
p("Replace: ", s.Replace("foo", "o", "0", 0)) //替换存在的字符一次 f0o
p("Split: ", s.Split("a-b-c-d-e", "-")) //指定字符分隔字符串:[a b c d e]
p("ToLower: ", s.ToLower("TEST"))
p("ToUpper: ", s.ToUpper("test"))
p("Len: ", len("hello"))
p("Char:", "hello"[1])
p("to []string:", s.Fields("hello")) //字符串变数组
p("去除首尾空白", s.TrimSpace(" hell o "))
}
计算字符串长度一一len()和RuneCountInString()
golang的内建函数len(),可以用来获取切片、字符串、通道(channel)等的长度。下面的代码可以用 len() 来获取字符串的长度。
len() 函数的返回值的类型为 int,表示字符串的 ASCII 字符个数或字节长度。这里的差异是由于golang的字符串都是以UTF-8格式保存,每个中文占用3个字节,因此使用 len() 获得两个中文文字对应的6个字节。
如果希望按习惯上的字符个数来计算,就需要使用 Go 语言中 UTF-8 包提供RuneCountInString() 函数,统计 Uncode 字符数量。
fmt.Println(utf8.RuneCountInString("忍者")) //2
fmt.Println(utf8.RuneCountInString("龙忍出鞘,fight!")) //11
一般游戏中在登录时都需要输入名字,而名字一般有长度限制。考虑到国人习惯使用中文做名字,就需要检测字符串 UTF-8 格式的长度。
总结
- ASCII 字符串长度使用 len() 函数。
- Unicode 字符串长度使用 utf8.RuneCountInString() 函数。
版权声明
本文为[昵称你猜8]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hudeyong926/article/details/124248050
边栏推荐
- Linux 7 silent installation of Oracle 11g
- SQL 数据类型
- Zhongshanghui ⺠ evolution of trading platform architecture: response to Apache shardingsphere
- You need to specify one of MySQL_ ROOT_ PASSWORD, MYSQL_ ALLOW_ EMPTY_ PASSWORD and MYSQ
- Getimagesize() function gets the inverse of the width and height of the picture
- [flutter topic] 125 illustrated autobiography ace_ ICON. Ttf Icon Library
- Da14580ble light LED
- Usage of stringstream
- L2-033 simple calculator (25 points)
- 【Go】程序流程控制
猜你喜欢

Leapftp: 425 failed to establish connection solution

DPU 芯片头部企业云豹智能加入 openEuler社区,为数字中国基础设施助力

Writing to remember the ~ goal and

linux7静默安装oracle11g

oracle19. 3 upgrade to 19.6

Creating student management system with MySQL

Command ‘yum‘ not found, but can be installed with: apt install yum

Oracle 11.0 under Windows Upgrade from 2.0.1 to Oracle 11.0 2.0.4

UKUI3 支持 openEuler 22.03 LTS

Softing datafeed OPC Suite: empowering industrial equipment to connect to the Internet of things
随机推荐
js老生常谈之this,constructor ,prototype
Ctyunos scale deployment of China Telecom and jointly build Tianyi cloud digital base with openeuler
最通俗易懂的依赖注入之服务容器与作用域
超越 iTerm!号称下一代 Terminal 终端神器,用完爱不释手!
DataGuard (ADG) spare database moves (migrates) data files
oracle19. 3 upgrade to 19.6
Beyond iterm! Known as the next generation terminal artifact, I can't put it down after using it!
stringstream的用法
5.嵌入式控制器(Embedded Controller,EC)学习 PS/2协议
[moistening C language] Beginner - start from scratch - 5 (modular design - function, value transmission and address transmission)
SQL 语法
Matplotlib tutorial 04 --- drawing commonly used graphics
企业级 Web 开发的挑战
shell获取随机数和echo -e '\033[字背景颜色;字体颜色m字符串\033[0m'
Interpretation of openeuler kernel technology | introduction to kernel interrupt subsystem
L3-007 ladder map (30 points) (condition DIJ)
二极管工作原理
Linux Installation Oracle 19C Full Version (graphics + silent installation)
Writing to remember the ~ goal and
Command ‘yum‘ not found, but can be installed with: apt install yum