golang常用函数
1.文件读写
package main import ( "fmt" "os" ) func main(){ yaml_str,err:=os.ReadFile("poc/1.yml") if err!=nil{ panic(err) } poc_old:=string(yaml_str)
os.WriteFile("poc/1.yml",[]byte("s_yaml_str1gdfgfdgdfg111"),os.ModePerm)
fmt.Println(poc_old) }
2.字符串处理
package main
import (
"fmt"
"strings"
)
func main(){
str1:="hello word"
fmt.Println(len(str1))
str2:=[]rune(str1)
for i:=0;i<len(str2);i++{
fmt.Printf("%c\n",str2[i])
}
str3:=strings.Contains(str1,"word")//是否包含关键词
fmt.Println(str3)
str4:=strings.Index(str1,"word")//索引位置
fmt.Println(str4)
str5 := strings.Trim("!hello !"," !")//去掉特殊字符
fmt.Println(str5)
str6 := strings.ToLower("Go")//转小写
str7 := strings.ToUpper("Go")//转大写
fmt.Println(str6,str7)
str8 := strings.TrimSpace(" tn hello ")//去掉空格
fmt.Println(str6,str8)
str9:=strings.Split("hello,wrold,ok",",")//分隔成数组
for j:=0;j<len(str9);j++{
fmt.Println(str9[j])
}
}
日期
package main import ( "fmt" "time" ) func main() { now := time.Now() fmt.Printf("now=%v not type=%T\n", now, now) dateStr := fmt.Sprintf("当前年月日: %d-%02d-%d %d:%d:%d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()) fmt.Println(dateStr) start := time.Now().Unix() time.Sleep(5*time.Second) end := time.Now().Unix() fmt.Printf("test 用时%v秒",end - start) 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() }