# Golang program to perform binary search on a slice of custom structs

In this Golang article, we are going to perform binary search on a slice of custom structs using iterative and recursive method. Binary search is a search algorithm used to find the position of a specific value within a sorted sequence of elements.

## Algorithm

• Step 1 − First, we need to import the fmt package.

• Step 2 − Create a custom Person structure with string type name and integer type age.

• Step 3 − Now, create a bSearch() function that is used to perform binary search on a slice of Person struct.

• Step 4 − It initializes the low and high variables to the beginning and end of the slice, respectively, and then a loop continues as long as low is less than or equal to high.

• Step 5 − In each iteration of the loop, it calculates the midpoint between low and high and compares the name field of the Person at that index to the key.

• Step 6 − If the name field is equal to the key, the function returns the index. If the name field is less than the key, it updates low to mid + 1. Otherwise, it updates high to mid - 1.

• Step 7 − Start the main() function. Inside the main() function, create a slice of Person structs and a key to search for.

• Step 8 − Now, call the bSearch() function and pass the struct and key to it as arguments.

• Step 9 − The resultant message is printed to the console. If the function returns -1, false is returned otherwise true is returned.

## Example 1

In this example, we will define a bSearch() function that is used to perform binary search on a slice of custom structs using iterative method.

package main

import "fmt"

type Person struct {
name string
age  int
}

func bSearch(people []Person, key string) int {
l := 0
h := len(people) - 1

for l <= h {
mid := (l + h) / 2

if people[mid].name == key {
return mid
} else if people[mid].name < key {
l = mid + 1
} else {
h = mid - 1
}
}
return -1
}

func main() {
people := []Person{
{"Amy", 15},
{"Bix", 20},
{"Jim", 25},
{"Ross", 40},
{"Siri", 45},
}

key := "Siri"

i := bSearch(people, key)

if i == -1 {
} else {
fmt.Printf("%s found at index %d\n", key, i)
}
}


## Output

Siri found at index 4


## Example 2

In this example, we will define a bSearch() function that is used to perform binary search on a slice of custom structs using recursive method.

package main

import "fmt"

type Person struct {
Name string
Age  int
}

func bSearch(people []Person, target Person, low int, high int) int {
if low > high {
return -1
}

mid := (low + high) / 2
if people[mid] == target {
return mid
} else if people[mid].Age < target.Age {
return bSearch(people, target, mid+1, high)
} else {
return bSearch(people, target, low, mid-1)
}
}

func main() {
people := []Person{
{"Angel", 10},
{"Carie", 12},
{"Simona", 15},
{"John", 17},
{"Sam", 20},
}

target := Person{"John", 17}
index := bSearch(people, target, 0, len(people)-1)

if index != -1 {
fmt.Printf("%s found at index %d\n", target.Name, index)
} else {
}
}


## Output

John found at index 3


## Conclusion

We have successfully compiled and executed a go language program to perform binary search on a slice of custom structs using iterative and recursive method along with two examples. In the first example, we have used the iterative method and in the second example, we have used the recursive method.

Updated on: 03-Apr-2023

515 Views