1.字符串函数

package main
import(
	"fmt"
	"strings"
	"strconv"
)

func main(){
	//string 常用函数
	//len 字符串长度 一个中文等于3个字节 所以是9
	str1 := len("string北")
	fmt.Println("str1 = ",str1)
	//结果9 

	//如果不用[]rune转换的话,那么输出的就会是字节 
	str2 := "string北京"
	str22 := []rune(str2)
	for i := 0; i < len(str22); i++{
		fmt.Printf("str22 = %c\n",str22[i])
	}
	/*
	结果:
	str22 = s
	str22 = t
	str22 = r
	str22 = i
	str22 = n
	str22 = g
	str22 = 北
	str22 = 京
	*/

	//字符串转整数 n,err := strconv.Atoi("12")
	//成功 err返回的是 nil 否则就是错误信息
	n,err := strconv.Atoi("111")
	if err != nil {
		fmt.Println("错误信息:",err)
	}else{
		fmt.Println("成功结果:",n)
	}

	//整数转字符串 n := strconv.Itoa(111)
	str3 := strconv.Itoa(123)
	fmt.Printf("成功结果:%v 数据类型:%T \n",str3,str3)
	//结果:成功结果:123 数据类型:string


	//字符串转[]byte :var bytes = []byte("hello go")
	var bytes = []byte("hello go")
	fmt.Printf("成功结果:%v 数据类型:%T \n",bytes,bytes)
	//结果:成功结果:[104 101 108 108 111 32 103 111] 数据类型:[]uint8

	//[]byte 转 字符串:str = string([]byte{97,98,99})
	bytes1 := string([]byte{97,98,99})
	fmt.Printf("成功结果:%v 数据类型:%T \n",bytes1,bytes1)
	//结果:成功结果:abc 数据类型:string


	//10进制 转换对应的 2,8,16进制:str := strconv.FormatInt(123,2) 返回对应的字符串
	str4 := strconv.FormatInt(123,2)
	fmt.Printf("成功结果:%v 数据类型:%T \n",str4,str4)
	//结果:成功结果:1111011 数据类型:string


	//查找子串是否在指定的字符串中:strings.Contains("seafood","foo")
	str5 := strings.Contains("seafood","foo")
	fmt.Printf("seafood 中 是否包含 foo: %v \n",str5)
	//seafood 中 是否包含 foo: true


	//统计一个字符串有几个指定的字符 strings.Count("ceheese","e")
	str6 := strings.Count("ceheese","e")
	fmt.Printf("ceheese 中有%v个e字符\n",str6)
	//结果:ceheese 中有4个e字符


	//不区分大小写的字符串比较(==是区分字符大小写的):fmt.Println(strings.EqualFold("abc","Abc"))
	str7 := strings.EqualFold("abc","Abc")
	fmt.Println("abc 和 Abc 的比较是",str7)
	//结果:abc 和 Abc 的比较是 true
	if "abc" == "Abc"{
		fmt.Println(true)
	}else{
		fmt.Println(false)	
	}
	//结果:abc 和 Abc 的比较是 false


	//返回字符串第一次初夏你得index值,如果没有返回-1 strings.Index("NLT_abc","abc")
	str8 := strings.Index("NLT_abc","a")
	fmt.Println(str8)	
	//结果4


	//返回子串在字符串最好一次出现的index,如没有返回-1:strings.LastIndex("go goland","go")
    str9 := strings.LastIndex("go golang","go")
	fmt.Println(str9)	
	//结果3


	//将指定的子串替换成另外一个子串:strings.Replace("go go hello","go","go语言",n) n可以指定你希望替换第几个,如果n=-1表示全部替换
    str10 := strings.Replace("go go hello","go","go语言",1)
	fmt.Println(str10)	
	//结果:go语言 go hello

  
	//按照指定的某个字符,为分割表示,将一个字符串拆分成字符串数组 strings.Split("hello,wrold,ok",",")
	str11 := strings.Split("hello,wrold,ok",",")
	fmt.Println(str11)	
	//结果:[hello wrold ok]


	//将字符串的字母进行大小写的转换:strings.ToLower("Go") strings.ToUpper("go")
	str12 := strings.ToLower("Go")//转小写
	str13 := strings.ToUpper("Go")//转大写
	fmt.Println(str12,str13)	
	//结果:go GO


	//将字符串左右两边的空格去掉:strings.TrimSpace(" tn a lone gopher ntrn ")
	str14 := strings.TrimSpace(" tn a lone gopher ntrn ")
	fmt.Println(str14)	
	//结果:tn a lone gopher ntrn

	
	//将字符串左右两边指定的字符去掉:strings.Trim("! hello!","!")
	str15 := strings.Trim("!hello !"," !")//参数二 是空格+!
	fmt.Println(str15)	
	//结果:hello


	//将字符串左边的字符去掉strings.TrimLeft("! hello!"," !")
	str16 := strings.TrimLeft("! hello!"," !")
	fmt.Println(str16)
	//结果hello!



	//将字符串右边的字符去掉strings.TrimRight("! hello!"," !")
    str17 := strings.TrimRight("! hello!"," !")
	fmt.Println(str17)
	//结果! hello


	//判断字符串是否以指定的字符串开头:strings.HasPrefix("ftp://192.168.10.1","ftp")
	str18 := strings.HasPrefix("ftp://192.168.10.1","ftp")
	fmt.Println(str18)
	//结果:true

    //判断字符串是否以指定的字符串结尾:strings.HasSuffix("NLT_adc.jpg","abc")
	str19 := strings.HasSuffix("NLT_adc.jpg","abc")
	fmt.Println(str19)
	//结果false
}

2.日期相关函数

package main
import(
	"fmt"
	"time"
)

func main(){
	//获取当前时间
	now := time.Now()
    fmt.Printf("now=%v not type=%T\n",now,now)
	//结果:now=2022-02-19 16:27:53.7429231 +0800 CST m=+0.002079501 not type=time.Time

	//通过new可以获取到年月日时分秒
	fmt.Printf("年=%v 月=%v 日=%v 时=%v 分=%v 秒=%v \n",now.Year(),int(now.Month()),now.Day(),now.Hour(),now.Minute(),now.Second())
    //结果年=2022 月=2 日=19 时=16 分=36 秒=47

	//格式化日期时间
	//方式一:
	fmt.Printf("当前年月日: %d-%d-%d %d:%d:%d\n",now.Year(),now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())
	//结果: 当前年月日: 2022-02-19 16:42:1
	dateStr := fmt.Sprintf("当前年月日: %d-%02d-%d %d:%d:%d",now.Year(),now.Month(),now.Day(),now.Hour(),now.Minute(),now.Second())
	fmt.Printf("dateStr=%v\n",dateStr)
	//结果:dateStr=当前年月日: 2022-02-19 16:45:33

	//方式二:
	//参数必须这么写。。。。。不知道为啥
	fmt.Printf(now.Format("2006-01-02 15:04:05"))
	fmt.Println()
	fmt.Printf(now.Format("2006-01-02"))
	fmt.Println()
	fmt.Printf(now.Format("15:04:05"))
	fmt.Println()



	//时间常量
	/*
	const (
		Nanosecond  Duration = 1  //纳秒
		Microsecond          = 1000 * Nanosecond  //微妙
		Millisecond          = 1000 * Microsecond //毫秒
		Second               = 1000 * Millisecond //秒
		Minute               = 60 * Second //分钟
		Hour                 = 60 * Minute //小时
	)
	*/
	i := 0
	for {
		i++
		fmt.Println(i)
		//休眠
		// time.Sleep(time.Second) //一秒输出一次
		// time.Sleep(time.Second * 0.1)//这样是不可以的,必须用下面这个方法
		time.Sleep(time.Millisecond * 100) //0.1毫秒执行一次 
		if i == 100{
			break
		}
	}





	//获取时间戳unix 和 纳秒时间戳unixnano
	fmt.Printf("unix时间戳=%v unixnano时间戳=%v",now.Unix(),now.UnixNano())
	//结果unix时间戳=1645261624 unixnano时间戳=1645261624882498000
}

操作案例

package main
import(
	"fmt"
	"strconv"
	"time"
)

func test(){
  str := ""	
  for i := 0; i < 100000; i++{
	str += "hello world" + strconv.Itoa(i)
  }
}

func main(){
   start := time.Now().Unix()
   test()
   end := time.Now().Unix()
   fmt.Printf("test 用时%v秒",end - start)
}