文章目录
2.4 常用的内置函数
2.4.1 字符串常用内置函数
https://golang.org/pkg/strings/
https://golang.org/pkg/strconv/
统计字符串长度的函数:
len(str)
字符串遍历,同时处理有中文的问题:
r:=[]rune(str)
字符串转整数:
strconv(str), strconv.ParseInt()
func strconvDemo() { v := "10" if s, err := strconv.Atoi(v); err == nil { fmt.Printf("%T, %v", s, s) }}
整数转字符串:
strconv(str)
func strItoa(){ i := 10 s := strconv.Itoa(i) fmt.Printf("%T, %v\n", s, s)}
字符串转成byte切片:
bytes := []byte("asdfgh")
)byte切片转换成字符串:
str4 := string([]byte{67, 68, 69, 70, 72})
10进制数转换为其他进制数(转换后为字符串类型):
str = strconv.FromatInt(123, 2)
func ten2others(){ s10 := strconv.FormatInt(v2, 10) fmt.Printf("%T, %v\n", s10, s10) s16 := strconv.FormatInt(v2, 16) fmt.Printf("%T, %v\n", s16, s16)}
查找字符串中是否包含指定字符串:
strings.Contains("seafood","food")
统计字符串中子字符串的个数:
strings.Count("aaaaaaa","a")
忽略大小写的字符串比较函数:
strings.EqualFold("Chinese","chinese")
查找子字符串第一次出现的位置:
strings.Index("stringsGolang","lang")
查找子字符串最后一次出现的位置:
strings.LastIndex("stringsGolang","lang")
替换指定字符串:
strings.Replace("Go Go Go", "Go", "Golang", 1)
按指定字符分割字符串:
strings.Split("Hello 北京 成都 上海 深圳", " ")
字符串大小写转换:
strings.ToLower、strings.ToUpper
去除字符串左右两端的指定字符(无法去除字符串内部的字符):
strings.Trim(" aaaaaaaaaC__", " _")
判断一个字符串是否以指定字符串开头:
func HasPrefix(s, prefix string) bool
判断一个字符串是否以指定字符串结尾:
func HasSuffix(s, suffix string) bool
2.4.2 常用的时间和日期相关函数
https://golang.org/pkg/time/
时间、日期相关函数需要导入time包
获取当前时间:
func getTime(){ //1. 获取当前时间 now := time.Now() fmt.Printf("Now = %v Type=%T", now, now) //Now = 2021-02-23 16:14:42.0211031 +0800 CST m=+0.000997301 Type=time.Time}
获取当前时间的详细信息:
func detail(){ //2.获取年月日时分秒 now := time.Now() fmt.Printf("年=%v\n", now.Year()) fmt.Printf("月=%v\n", now.Month()) fmt.Printf("日=%v\n", int(now.Day())) fmt.Printf("时=%v\n", now.Hour()) fmt.Printf("分=%v\n", now.Minute()) fmt.Printf("秒=%v\n", now.Second())}
格式化时间格式:(format的字符串数值是固定的,必须是2006/01/02 15:04:05)
func timeFormat(){ now := time.Now() fmt.Println(now.Format("2006/01/02 15:04:05")) fmt.Println(now.Format("2006-01-02")) fmt.Println(now.Format("15:04:05"))}
时间常量:
const ( Nanosecond Duration = 1 Microsecond = 1000 * Nanosecond Millisecond = 1000 * Microsecond Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute)const ( ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000")
结合sleep来使用时间常量
func sleepDemo(){ for { fmt.Println(time.Now().Second()) time.Sleep(time.Millisecond * 100) }}
获取当前unix时间戳
func getUnixTime(){ fmt.Println(time.Now().Unix()) fmt.Println(time.Now().UnixNano())}
2.4.3 内置函数
https://golang.org/pkg/builtin/ /内建函数/
len()
: 获取字符串长度的函数:new()
: 用来分配内存,主要用来分配值类型,如int, float64,make()
: 用来分配内存,主要分配引用类型的内存, 如切片,channel, map等
func main() { num1 := 100 fmt.Printf("num1的类型为:%T, num1的值为:%d, num1的地址为:%p\n", num1, num1, &num1) num2 := new(int) /*new的返回值是一个指针*/ fmt.Printf("num2的类型为:%T, num2的值为:%d, num2的地址为:%p\n", num2, *num2, &num2)}