GO—常用函數

1,正則表達式

引用:impot:regexp

match,_:=regex.MatchString("正則表達式",“要匹配的字符串")(是否匹配,錯誤)

註冊正則:r :=regexp.Complie("正則表達式")

r.MatchString("串")  返回是否匹配

func reg() {
	match, _ := regexp.MatchString("chenghan", "100chenghan")
	fmt.Println(match)
	r, _ := regexp.Compile("chenghan")
	fmt.Println(r.MatchString("chenghan"))
	fmt.Println(r.FindString("11110chenghan"))
	fmt.Println(r.FindStringIndex("11110chenghan"))
	fmt.Println(r.FindStringSubmatch("11110chenghan"))
}

2,json的序列化與反序列化.

引用:import "encoding/json"

stu:=&Student{1,"chenghan"} 創建對象

a:=json.Marshal(stu)  對象序列化程串串

stu1:=&Student{}創建空對象

json.UnMarshal([]byte(a),stu)  串串序列化程對象


3,time的格式化

time.now.Format("2006-01-02 15:04:05")  //效果等同 yyyy-MM-dd hh:mm:ss

time.now.unix()  獲取1701年至今的秒

time.now.nuixNors() 獲取1701年至今的納秒


4,rand 隨機數

rand.intN(int)  返回int以內的隨機數。但是有與隨機種子是相同的,SO每次返回的都是同一個數列

r := rand.newSource(int64(time.Now().unixNano()))

i :=r.New(r).IntN(40) 用新的種子進行序列號好的生成


5,scan 瀏覽

scanner := bufio.newScanner(os.stdin)  //初始化掃描函數

scanner.scan()  //掃描

fmt.printf(scanner.text()) //輸出掃描到的數據

6,flag

import flag

time = flag.int("runtimes",10," an int")

name = flag.String("name","chenghan"," a string")

isOpen = flag.bool("runtimes",true," a bool")



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章