其他工具
in
func elementIsInSlice(element int, elements []int) (isIn bool) {
for _, item := range elements {
if element == item {
isIn = true
return
}
}
return
}
func elementIsInSlice2(element string, elements []string) (isIn bool) {
for _, item := range elements {
if element == item {
isIn = true
return
}
}
return
}
func main() {
li:=[]string{"a","b","c"}
if elementIsInSlice2("a",li){
fmt.Println("在里面")
}
}
字符串反转
func main() {
fmt.Println(reverseString("abc"))
}
//字符串反转
func reverseString(str string) string {
strRune := []rune(str)
l := len(strRune)
for i := 0; i < l/2; i++ {
strRune[i], strRune[l-i-1] = strRune[l-i-1], strRune[i]
}
return string(strRune)
}
随机数
func RandomInt(length int) (str string) {
var arr []byte = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
size := len(arr)
for i := 0; i < length; i++ {
str += string(arr[r.Intn(size)])
}
return
}
选择了IT,必定终身学习