GO—常用函数

来源:互联网 发布:java构造器 编辑:程序博客网 时间:2024/06/06 00:44

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")



 

0 0
原创粉丝点击