Go语言顺序查找

package main

import "fmt"
// 顺序查找
func orderSearch(str string, arr [5]string) (b bool){
	var index = -1
	for i := 0; i < len(arr); i++ {
		if str == arr[i] {
			index = i
		}
	}

	if index != -1 {
		return true
	} else {
		return false
	}
}

func main() {
	arr := [5]string{"a", "b", "c", "d", "e"}
	result := orderSearch("c", arr)
	// result := orderSearch("h", arr) // false
	fmt.Println("result=", result) // true
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/113038933