1. math包
fmt.Printf("%#v\n",err) // (*os.PathError)(nil)
fmt.println(math.abs(-19)) // 取絕對(duì)值
fmt.Println(math.Floor(3.14)) // 向下取整
fmt.Println(math.Ceil(3.14)) //向上取整
fmt.Println(math.Round(3.3478)) //就近取整
fmt.Println(math.Round(3.3478*100)/100) //四舍五入
fmt.Println(math.Mod(11, 3)) //取余數(shù)
fmt.Println(math.Pow(2, 3)) //計(jì)算次方
fmt.Println(math.Pow10(2)) //計(jì)算10次方
fmt.Println(math.Max(2, 10)) //取較大值
fmt.Println(math.Min(2, 10)) //取較小值
2. strconv包
//整型轉(zhuǎn)字符串
v1 := strconv.Itoa(123)
//字符串轉(zhuǎn)int整型
v2, _ := strconv.Itoa("123")
//字符串轉(zhuǎn)int64整型
v3, _ := strconv.ParseInt("123")
//字符串轉(zhuǎn)bool值
//True: "1", "t", "T", "true", "True", "TRUE"
//False: "0", "f", "F", "false", "False", "FALSE"
//其他報(bào)錯(cuò)滚朵,返回錯(cuò)誤
v4, _ := strconv.ParseBool("1")
//布爾轉(zhuǎn)字符串
v5 := strconv.FormatBool(true)
3. strings
常規(guī)功能
// 常規(guī)功能
//用于比較兩個(gè)字符串群发。它用于按字典順序比較兩個(gè)字符串,
//如果字符串相等(s1 == s2)辨嗽,則返回0
//如果字符串1大于字符串2(s1> s2)羹蚣,則返回1。
//如果字符串1小于字符串2夹纫,則返回-1(s1 <s2)
fmt.Println(strings.Compare("ab", "ab"))
//判斷字符串s是否包含子串substr
fmt.Println(strings.Contains("seafood", "foo"))//true
//返回字符串s中有幾個(gè)不重復(fù)的sep子串拄显。
fmt.Println(strings.Count("cheese", "e")) //3
//返回將所有字母都轉(zhuǎn)為對(duì)應(yīng)的標(biāo)題版本的拷貝将饺。
fmt.Println(strings.ToTitle("Title")) //TITLE
//返回將所有字母都轉(zhuǎn)為對(duì)應(yīng)的大寫(xiě)版本的拷貝
fmt.Println(strings.ToUpper("Title")) //TITLE
//返回將所有字母都轉(zhuǎn)為對(duì)應(yīng)的小寫(xiě)版本的拷貝
fmt.Println(strings.ToLower("Title")) //title
//判斷s是否有前綴字符串prefix
fmt.Println(strings.HasPrefix("Hello,Ethan", "He"))
//判斷s是否有后綴字符串suffix卵史。
fmt.Println(strings.HasSuffix("Hello,Ethan", "an"))
字符串切割
//字符串轉(zhuǎn)切片
fmt.Println(strings.Split("a,b,c", ",")) //[ a b c]
fmt.Println(strings.SplitAfter("a,b,c", ",")) //[a, b, c]
fmt.Println(strings.SplitAfterN("a,b,c", ",", -1))
// 最后一個(gè)參數(shù) n 就是返回的切片元素個(gè)數(shù)
fmt.Println(strings.SplitAfterN("a,b,c", ",", 2))
// 用 len 可以驗(yàn)證战转,其實(shí)就是將這段 str 分割成多少"段"
fmt.Println(len(strings.SplitAfterN("/usr/local/bin/abs/sdk", "/", 3)))
獲取 substr 索引
//子串sep在字符串s中第一次出現(xiàn)的位置,不存在則返回-1
fmt.Println(strings.Index("ethan,thanks", "than")) //1
//子串sep在字符串s中最后一次出現(xiàn)的位置以躯,不存在則返回-1槐秧。
fmt.Println(strings.LastIndex("ethan,thanks", "than")) //6
字符串拼接
//切片轉(zhuǎn)字符串
//將一系列字符串連接為一個(gè)字符串,之間用sep來(lái)分隔寸潦。
fmt.Println(strings.Join([]string{"usr", "local", "bin"}, "/")) // usr/local/bin
Trim 系列函數(shù)
//返回將s前后端所有cutset包含的utf-8碼值都去掉的字符串。
fmt.Println(strings.Trim(" abc ", "a ")) //bc
fmt.Println(strings.TrimSpace(" mysql ")) //mysql
fmt.Println(strings.TrimLeft("!!!Ethan!!!", "!")) //Ethan!!!
fmt.Println(strings.TrimRight("!!!Ethan!!!", "!")) //!!!Ethan
替換 Replace 函數(shù)
// 替換所有
fmt.Println(strings.ReplaceAll("/usr/local/mysql/", "/", "\\")) // \usr\local\mysql\
// Replace 多了一個(gè)傳入?yún)?shù)(替換 n 次)
fmt.Println(strings.Replace("/usr/local/mysql/", "/", "\\", 2)) // \usr\local/mysql/
// 或者創(chuàng)建一個(gè) *strings.Replacer 類型對(duì)象(是個(gè)結(jié)構(gòu)體)社痛,需要傳入替換內(nèi)容
Replacer := strings.NewReplacer("/", ":")
splitStr := strings.SplitAfterN("/usr/local/mysql", "/", 2)[1]
res := Replacer.Replace(splitStr)
fmt.Println(res) //usr:local:mysql
4. json包
//序列化(轉(zhuǎn)json)
type Person struct{
Name string `json : "name"`
Age int `json : "age"`
}
v1 := []interface{}{
//字符串
"zhangsan",
//整數(shù)
18,
//bool
true,
//浮點(diǎn)數(shù)
4.13,
//map
map[string]interface{}{
"adress" : "beijing",
},
//結(jié)構(gòu)體
Person{
"zhangsan",
18
}
}
res, _ json.Marshal(v1)
data := string(res)
//反序列化
str := `["zhangsan", 18, true, 4.13, {"age":18,"name":"zhangsan"}]`
var value []interface{}
json.Unmarshal([]byte(str), &value)
fmt.Println(value)
5. time包
//獲取當(dāng)前時(shí)間
curDate := time.Now() //2022-08-22 12:54:12.507845 +0800 CST m=+0.000176922
//第一種见转,格式化時(shí)間(年月日時(shí)分秒)
date := curDate.Format("2006-01-02 15:04:05") // 2022-08-22 12:44:51
//fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
//第二種, 分別獲取年月日,時(shí)分秒
fmt.Println(
curDate.YearDay(), int(curDate.Month()), curDate.Day(),
curDate.Hour(), curDate.Minute(), curDate.Second()
)
//2023 February 6 19 19 40 //月份不強(qiáng)轉(zhuǎn)的話是英文的月份
//2023 2 6 19 22 52
//用字符串接收
strDate := fmt.Sprintf("當(dāng)前年月日 %d-%d-%d %d:%d:%d \n", curDate.Year(), int(curDate.Month()), curDate.Day(), curDate.Hour(), curDate.Minute(), curDate.Second())
fmt.Println(strDate)
//當(dāng)前年月日 2023-02-06 19:28:41
//utc時(shí)間
time.Now().UTC()
//本地時(shí)間(東八區(qū))
time.Now().Local()
// 默認(rèn)UTC
loc, err := time.LoadLocation("")
// 服務(wù)器設(shè)定的時(shí)區(qū)蒜哀,一般為CST
loc, err := time.LoadLocation("Local")
// 美國(guó)洛杉磯PDT
loc, err := time.LoadLocation("America/Los_Angeles")
// 獲取指定時(shí)區(qū)的時(shí)間點(diǎn)
local, _ := time.LoadLocation("America/Los_Angeles")
fmt.Println(time.Date(2018,1,1,12,0,0,0, local))
//字符串類型轉(zhuǎn)換成time類型
time1, err := time.Parse("2006-01-02", "2022-04-05")
//創(chuàng)建一個(gè)時(shí)間(Time類型)
t2 := time.Date(2022, 8,22,13,01,1,0, time.Local)
fmt.Println(t2)
//獲取當(dāng)前時(shí)間戳
t1 := time.Now().Unix() //單位秒 1661145016
t2 := time.Now().UnixNano()// 單位納秒 1661145251807445000
//時(shí)間常量
const (
Nanosecond Duration = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
//常量的作用:比如需要獲取100毫秒(0.1秒)
time.Sleep(time.Millisecond * 100) // 只能用微妙*100斩箫, 不可以用1秒/10
//time.Sleep(time.Second/10) 錯(cuò)誤的