Go 实现历史搜索记录
package main
import (
"fmt"
)
func main() {
keyword := "广州"
arr := [10]string{"合肥", "上海", "wuxi","深圳","湖南","fuyang","usa"}
fmt.Println(arr)
index := 9
//查找key的下标位置,存在替换index默认值
for k, v := range arr{
if v == keyword{
index = k
break
}
}
//根据index的下标位置,左侧元素后移一位
for i := index; i > 0; i-- {
arr[i] = arr[i-1]
}
//替换下标为0的值
arr[0] = keyword
fmt.Println(arr)
}