Go语言的常用函数
导包
| import "strconv" |
| import "strings" |
| import "time" |
字符串函数
len()统计字符串长度
golang编码统一是utf8编码 --汉字占三个字节
| func len (string) (int){...} |
| package main |
| import "fmt" |
| func main() { |
| str:="hello背" |
| fmt.Println("str=",len(str)) |
| } |
strconv.Atoi 将字符串转为整数
| |
| func IntString(str string)(){ |
| value, err:= strconv.Atoi(str) |
| if err!=nil{ |
| fmt.Printf("转换失败:%v\n", err) |
| }else { |
| fmt.Printf("转换成功的值:%v\n", value) |
| fmt.Printf("转换成功的类型:%T\n", value) |
| } |
| } |
整数转字符串strconv.Itoa()
strconv.Itoa()
| |
| |
| func IntToString(number int)(){ |
| |
| str := strconv.Itoa(number) |
| |
| fmt.Printf("%v %t\n", str,str) |
| |
| } |
r = []rune (str) 中文的变历
对含有中文的字符串遍历
| func ChineseEachChar(str string)(){ |
| r := []rune(str) |
| fmt.Printf("\n*****************\n") |
| for i:=1;i<len(r);i++{ |
| fmt.Printf("字符:%c\n", r[i]) |
| } |
| } |
对不含有中文的字符串遍历可用
| str1 :="hello" |
| for i:=0;i<len(str1);i++{ |
| fmt.Printf("字符%c\n", str1[i]) |
| } |
将字符串转为 []byte
| var bytes = [] byte("hello") |
将byte转为string
| str := strings([]byte{1,2,3,4}) |
查询子串是否在指定的子串
| strings.Contains("seafood","food") |
统计一个子串在另一个子串中存在多少个
| |
| func CountNumber(str1 string ,str2 string)(int){ |
| return strings.Count(str1,str2) |
| } |
字符串的比较(区分大小写与不区分大小写)
== 区分大小写
string.EqualFold(string,string) :不区分大小写
| b:=string.EqualFold(string01,string02) |
返回子串第一次出现的位置
| strings.index(str1,str2) |
| |
| strings.index("as_dd","_dd") |
返回最后一次出现的位置
| strings.Lastindex(str1,str2) |
| |
| strings.Lastindex("as_dd","_dd") |
將子串替換
| strings.Replace("go go hello","go","go語言",n) |
将字符串进行切割
| strings.split("hello,world,ok",",") |
字符串的首字母大小写
| string.ToLower("Go") |
| string.ToUpper("go") |
日期时间函数
获取年月日时分秒
| import ( |
| "fmt" |
| "time" |
| ) |
| func main() { |
| now := time.Now() |
| fmt.Printf("current time:%v\n", now) |
| year := now.Year() |
| month := now.Month() |
| day := now.Day() |
| hour := now.Hour() |
| minute := now.Minute() |
| second := now.Second() |
| fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second) |
| } |
获取时间戳
| package main |
| import ( |
| "fmt" |
| "time" |
| ) |
| func main() { |
| now := time.Now() |
| timestamp1 := now.Unix() |
| timestamp2 := now.UnixNano() |
| fmt.Printf("现在的时间戳:%v\n", timestamp1) |
| fmt.Printf("现在的纳秒时间戳:%v\n", timestamp2) |
| } |
获取当前是星期几
time 包中的 Weekday 函数能够返回某个时间点所对应是一周中的周几,示例代码如下
| package main |
| |
| import ( |
| "fmt" |
| "time" |
| ) |
| |
| func main() { |
| |
| t := time.Now() |
| fmt.Println(t.Weekday().String()) |
| } |
时间格式化
时间类型有一个自带的 Format 方法进行格式化,需要注意的是Go语言中格式化时间模板不是常见的Y-m-d H:M:S
而是使用Go语言的诞生时间 2006 年 1 月 2 号 15 点 04 分 05 秒。
提示:如果想将时间格式化为 12 小时格式,需指定 PM。
| package main |
| import ( |
| "fmt" |
| "time" |
| ) |
| func main() { |
| now := time.Now() |
| |
| |
| fmt.Println(now.Format("2006-01-02 15:04:05.000 Mon Jan")) |
| |
| fmt.Println(now.Format("2006-01-02 03:04:05.000 PM Mon Jan")) |
| fmt.Println(now.Format("2006/01/02 15:04")) |
| fmt.Println(now.Format("15:04 2006/01/02")) |
| fmt.Println(now.Format("2006/01/02")) |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具