title: Golang Binary search
body: <p>I'm practicing an interview algorithm, now coding it in Go. The purpose is to practice basic interview algorithms, and my skills in Go. I'm trying to perform a Binary search of an array of numbers.</p>
  
  <pre><code>package main
  
  import "fmt"
  
  func main() {
      searchField := []int{2, 5, 8, 12, 16, 23, 38, 56, 72, 91}
      searchNumber := 23
  
      fmt.Println("Running Program")
      fmt.Println("Searching list of numbers: ", searchField)
      fmt.Println("Searching for number: ", searchNumber)
  
      numFound := false
      //searchCount not working. Belongs in second returned field
      result, _ := binarySearch2(searchField, len(searchField), searchNumber, numFound)
      fmt.Println("Found! Your number is found in position: ", result)
      //fmt.Println("Your search required ", searchCount, " cycles with the Binary method.")
  }
  
  func binarySearch2(a []int, field int, search int, numFound bool) (result int, searchCount int) {
      //searchCount removed for now.
      searchCount, i := 0, 0
      for !numFound {
          searchCount++
          mid := i + (field-i)/2
          if search == a[mid] {
              numFound = true
              result = mid
              return result, searchCount
          } else if search &gt; a[mid] {
              field++
              //i = mid + 1 causes a stack overflow
              return binarySearch2(a, field, search, numFound)
          }
          field = mid
          return binarySearch2(a, field, search, numFound)
      }
      return result, searchCount
  }
  </code></pre>
  
  <p>The main problems I'm coming across are: </p>
  
  <p>1) When the number is higher in the list than my mid search, am I truly continuing a binary search, or has it turned to a sequential? How can I fix that? The other option I've placed has been commented out because it causes a stack overflow.</p>
  
  <p>2) I wanted to add a step count to see how many steps it takes to finish the search. Something to use with other search methods as well. If I print out the search count as is, it always reads one. Is that because I need to return it (and therefore call for it in the header) in the method?</p>
  
  <p>I understand Go has methods that streamline this process. I'm trying to increase my knowledge and coding skills. I appreciate your input.</p>