import "runtime/debug"
fmt.Printf("%s", debug.Stack())
// map
var a map[string]string
a = make(map[string]string)
// slice
var b = make([]string, 0)
// map
tmpMap := map[int]string{0 : "a", 1 : "b"}
// slice
slis := []int{1,2,3,4,5,6,7,8}
// array
nums := [...]int{1,2,3,4,5,6,7,8}
aaa := sync.Map{}
ids := []string{}
aaa.Store("1", ids)
info, ok := aaa.Load("1")
if ok {
ids = info.([]string)
fmt.Println(ids)
}
ids = append(ids, "2")
ids.Store("1", ids)
array
for k, v:= range nums{
// k 为下标,v 为对应的值
fmt.Println(k, v, " ")
}
slice
for k, v:= range slis{
// k 为下标,v 为对应的值
fmt.Println(k, v, " ")
}
map
for k, v:= range tmpMap{
// k 为键值,v 为对应值
fmt.Println(k, v, " ")
}
模拟while循环
for i < l {
if i == l - 1 {
break
} else {
i += 1
}
}
// 切分
s := "/home/a/a_2022.09.08.1019.dat"
a := strings.Split(s, ".")
// 拼接
b := a[:len(a)-1]
c := strings.Join(b, ".") + ".tar.gz
// 包含
strings.Contains(fileName, ".tar.gz")
// interface转string
// value是interface类型
value.(string)
// interface to slice
newValue := v.([]string)
// interface to pointer
x := v.(*unsafe.Pointer)
// float转string
strScore := strconv.FormatFloat(float64(score), 'f', 0, 32)
str_ := fmt.Sprintf("%f", floatVar)
// int转string
str := strconv.Itoa(score)
int_ := fmt.Sprintf("%d", intVar)
// string转int
int, err := strconv.Atoi(string)
// string转long
n, _ := strconv.ParseInt(tid, 10, 64)
// []byte转string
a := string(b)
// string转[]byte
a := []byte(b)
// 获取当前时间
t := time.Now()
// 获取时间戳
t.Unix()
// unix时间戳转标准时间字符串
timeString := t.Unix().Format("2006-01-02 15:04:05")
// 标准时间字符串转unix时间戳
loc, _ := time.LoadLocation("Asia/Shanghai")
tt, _ := time.ParseInLocation("2006-01-02 15:04:05", timeString, loc)
timeStamp := tt.Unix()
//获取两天前的时间
currentTime := time.Now()
oldTime := currentTime.AddDate(0, 0, -2)