{"id":780,"date":"2026-10-06T15:00:00","date_gmt":"2026-10-06T09:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/"},"modified":"2026-11-29T01:29:36","modified_gmt":"2026-11-28T19:59:36","slug":"go-searching-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/","title":{"rendered":"Go Searching Programs: Linear Search, Binary Search, Two Sum"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go searching programs find a value in a collection, such as an order ID in a list or a word in a dictionary. In this post, you will write fourteen search programs with their exact output, from simple linear search to binary search, first and last position, rotated arrays and the famous two-sum problem. Then you will practice the search questions that interviewers use to test your thinking about speed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post follows our <a href=\"https:\/\/smitabharat.com\/blog\/go-sorting-programs\/\">Go sorting programs<\/a>, because binary search only works on sorted data. You will also use the slice tools from our <a href=\"https:\/\/smitabharat.com\/blog\/go-slice-programs\/\">Go slice programs<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Searching Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go searching programs look through data to find where a value is, or to prove that it is not there. The two main methods are linear search, which checks items one by one, and binary search, which halves the search space each step.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of finding a word in a dictionary. You could read every page from the start, which is linear search. Or you open the middle, see whether your word comes before or after, and keep halving. That is binary search, and it finds any word in a few steps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Searching Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Search is one of the most common jobs a program does, so Go searching programs are worth learning well. For example, a login checks if a user exists, a shop finds a product by ID and a game looks up a high score. Interviewers also love binary search, because a tiny mistake in the loop causes wrong answers or endless loops.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Speed:<\/strong> binary search on 1 million items needs only about 20 checks.<\/li>\n\n\n<li><strong>Interviews:<\/strong> first and last position, rotated array and two-sum are very common.<\/li>\n\n\n<li><strong>Real code:<\/strong> <code>slices.Index<\/code>, <code>slices.BinarySearch<\/code> and maps cover most daily needs.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Searching Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Binary search keeps two indexes, <code>low<\/code> and <code>high<\/code>, around the part that might hold the answer. Each step checks the middle item and throws away the half that cannot hold the value.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-1-1024x640.png\" alt=\"Go searching programs diagram showing binary search halving a sorted slice\" class=\"wp-image-760\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Binary search checks the middle item and keeps only the half that can still hold the target.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the search methods in this post.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Needs sorted data<\/th><th>Time<\/th><th>Best for<\/th><\/tr><\/thead><tbody><tr><td>Linear search<\/td><td>No<\/td><td>O(n)<\/td><td>Small or unsorted lists<\/td><\/tr><tr><td>Binary search<\/td><td>Yes<\/td><td>O(log n)<\/td><td>Large sorted lists<\/td><\/tr><tr><td>Map lookup<\/td><td>No, but needs a map<\/td><td>O(1) average<\/td><td>Many lookups by key<\/td><\/tr><tr><td>Staircase search<\/td><td>Sorted rows and columns<\/td><td>O(rows + cols)<\/td><td>Sorted 2D grids<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Linear Go Searching Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Linear Go searching programs work on any data, sorted or not.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Check If a Name Is in the List<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the easiest search of all. You look at each item and answer yes or no.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ := creates a variable and gives it a value in one go.\n    guests := []string{\"Asha\", \"Ravi\", \"Meera\"}\n    name := \"Ravi\"\n\n    found := false \/\/ we have not found the name yet\n    \/\/ range gives the index and the value on each turn. _ skips the index.\n    for _, guest := range guests {\n        if guest == name {\n            found = true\n            break \/\/ stop looking, we have the answer\n        }\n    }\n    fmt.Println(\"Is\", name, \"invited?\", found)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Is Ravi invited? true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop compares each guest with <code>name<\/code>. When it finds a match, it sets <code>found<\/code> to true and stops with <code>break<\/code>. If no guest matched, <code>found<\/code> would stay false.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Linear Search<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ linearSearch returns the index of target in items, or -1 if it is missing.\nfunc linearSearch(items []int, target int) int {\n    for index, value := range items {\n        if value == target {\n            return index \/\/ first match found, so stop here\n        }\n    }\n    return -1 \/\/ -1 means \"not found\", because no real index is negative\n}\n\nfunc main() {\n    ids := []int{104, 87, 230, 15, 99}\n    fmt.Println(linearSearch(ids, 230))\n    fmt.Println(linearSearch(ids, 500))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2\n-1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The function checks the items from left to right. It returns the index of the first match, or -1 when the value is missing. This is the same rule that <code>slices.Index<\/code> follows.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Find All Positions of a Value<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    marks := []int{70, 85, 70, 90, 70}\n    target := 70\n\n    var positions []int \/\/ an empty slice to collect the answers\n    for index, mark := range marks {\n        if mark == target {\n            \/\/ append adds the index at the end of positions.\n            positions = append(positions, index)\n        }\n    }\n    fmt.Println(positions, len(positions))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[0 2 4] 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This time the loop does not stop at the first match. Each time a mark equals 70, you add its index to <code>positions<\/code>. So the result holds every place where 70 appears.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Search with slices.Index and IndexFunc<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n    \"strings\"\n)\n\n\/\/ startsWithM reports whether a city name begins with the letter M.\nfunc startsWithM(city string) bool {\n    return strings.HasPrefix(city, \"M\")\n}\n\nfunc main() {\n    cities := []string{\"Pune\", \"Delhi\", \"Goa\", \"Mumbai\"}\n    fmt.Println(slices.Index(cities, \"Goa\"))        \/\/ position of \"Goa\"\n    fmt.Println(slices.Contains(cities, \"Chennai\")) \/\/ true or false\n\n    \/\/ IndexFunc returns the first position where startsWithM returns true.\n    index := slices.IndexFunc(cities, startsWithM)\n    fmt.Println(index, cities[index])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2\nfalse\n3 Mumbai<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.Index<\/code> and <code>slices.Contains<\/code> search for an exact value. <code>IndexFunc<\/code> returns the first item where your function returns true. It is handy when you search by a rule instead of an exact value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Binary Go Searching Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Binary Go searching programs need sorted data, but they are much faster on large lists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Iterative Binary Search<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ binarySearch finds target in a sorted slice and returns its index, or -1.\nfunc binarySearch(numbers []int, target int) int {\n    low := 0\n    high := len(numbers) - 1\n\n    \/\/ Keep going while there is still a part left to check.\n    for low &lt;= high {\n        \/\/ mid is the middle index of the part we are checking.\n        mid := low + (high-low)\/2\n        if numbers[mid] == target {\n            return mid\n        } else if numbers[mid] &lt; target {\n            low = mid + 1 \/\/ target is bigger, so drop the left half\n        } else {\n            high = mid - 1 \/\/ target is smaller, so drop the right half\n        }\n    }\n    return -1\n}\n\nfunc main() {\n    sorted := []int{3, 8, 15, 23, 42, 57, 61, 78}\n    fmt.Println(binarySearch(sorted, 42))\n    fmt.Println(binarySearch(sorted, 3))\n    fmt.Println(binarySearch(sorted, 50))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4\n0\n-1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each turn checks the middle item. If it is too small, you move <code>low<\/code> past it, and if it is too big, you move <code>high<\/code> below it. You write <code>low + (high-low)\/2<\/code>, because it never overflows on huge slices.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Recursive Binary Search<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ search looks for target between index low and index high.\n\/\/ It calls itself on the smaller half (recursion) until it finds the target.\nfunc search(numbers []int, target, low, high int) int {\n    \/\/ Nothing left to check, so the target is missing.\n    if low &gt; high {\n        return -1\n    }\n    mid := low + (high-low)\/2\n    if numbers[mid] == target {\n        return mid\n    }\n    if numbers[mid] &lt; target {\n        return search(numbers, target, mid+1, high) \/\/ search the right half\n    }\n    return search(numbers, target, low, mid-1) \/\/ search the left half\n}\n\nfunc main() {\n    numbers := []int{2, 4, 6, 8, 10, 12}\n    fmt.Println(search(numbers, 10, 0, len(numbers)-1))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each call checks the middle item, then calls itself on one half. The first middle is 6, so the next call searches the right half. This recursive version is easy to read, but the loop version uses less memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: slices.BinarySearch with Insert Position<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    prices := []int{100, 250, 400, 650}\n\n    \/\/ BinarySearch returns two values: the position, and found (true if the value is there).\n    position, found := slices.BinarySearch(prices, 400)\n    fmt.Println(position, found)\n\n    \/\/ 300 is missing, but position tells you where it would go.\n    position, found = slices.BinarySearch(prices, 300)\n    fmt.Println(position, found)\n    prices = slices.Insert(prices, position, 300)\n    fmt.Println(prices)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 true\n2 false\n[100 250 300 400 650]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first search finds 400 at index 2. When the value is missing, <code>BinarySearch<\/code> still returns the index where it should go. So you can insert 300 there and keep the slice sorted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: First and Last Position of a Value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With duplicates, a normal binary search may land on any copy. Here you find the first and last copies.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\nfunc main() {\n    numbers := []int{1, 3, 3, 3, 5, 7}\n    target := 3\n\n    \/\/ Step 1: first index where the number is 3 or more.\n    first := sort.SearchInts(numbers, target)\n\n    \/\/ Step 2: first index where the number is more than 3.\n    \/\/ sort.Search asks our small function \"is the rule true at index i?\".\n    afterLast := sort.Search(len(numbers), func(i int) bool {\n        return numbers[i] &gt; target\n    })\n    last := afterLast - 1 \/\/ one step back is the last 3\n\n    \/\/ Step 3: make sure the target really is in the slice.\n    if first &lt; len(numbers) &amp;&amp; numbers[first] == target {\n        fmt.Println(\"First:\", first, \"Last:\", last)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>First: 1 Last: 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sort.Search<\/code> finds the first index where your rule becomes true. So &#8220;first item greater than 3&#8221; is index 4, and one step back is the last 3. The check at the end makes sure 3 really exists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Search in a Rotated Sorted Array<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A rotated array looks like <code>[40 50 60 10 20 30]<\/code>. One half is always sorted, so you check which half and search there.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ searchRotated finds target in a sorted slice that was rotated, or returns -1.\nfunc searchRotated(numbers []int, target int) int {\n    low := 0\n    high := len(numbers) - 1\n    for low &lt;= high {\n        mid := low + (high-low)\/2\n        if numbers[mid] == target {\n            return mid\n        }\n        if numbers[low] &lt;= numbers[mid] {\n            \/\/ The left half is sorted. Is the target inside it?\n            if target &gt;= numbers[low] &amp;&amp; target &lt; numbers[mid] {\n                high = mid - 1\n            } else {\n                low = mid + 1\n            }\n        } else {\n            \/\/ The right half is sorted. Is the target inside it?\n            if target &gt; numbers[mid] &amp;&amp; target &lt;= numbers[high] {\n                low = mid + 1\n            } else {\n                high = mid - 1\n            }\n        }\n    }\n    return -1\n}\n\nfunc main() {\n    numbers := []int{40, 50, 60, 10, 20, 30}\n    fmt.Println(searchRotated(numbers, 20), searchRotated(numbers, 50), searchRotated(numbers, 35))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>4 1 -1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On each turn, you find which half is sorted by comparing its two ends. Then you check if the target falls inside that sorted half. If it does, you search there, otherwise you search the other half.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Clever Go Searching Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go searching programs use maths or a map instead of a plain search.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Look Up a Phone Number by Name<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A map finds a value by its key in one step, with no loop at all.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ A map links each name (key) to a phone number (value).\n    phoneBook := map[string]string{\n        \"Asha\": \"98200 11111\",\n        \"Ravi\": \"98200 22222\",\n    }\n\n    \/\/ A map lookup gives two values: the value, and ok (true if the key exists).\n    number, ok := phoneBook[\"Ravi\"]\n    fmt.Println(number, ok)\n\n    number, ok = phoneBook[\"Kiran\"]\n    if !ok {\n        fmt.Println(\"Kiran is not in the phone book\")\n    } else {\n        fmt.Println(number)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>98200 22222 true\nKiran is not in the phone book<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first lookup finds Ravi, so <code>ok<\/code> is true. The second name is missing, so <code>ok<\/code> is false and the program prints a message. Always check <code>ok<\/code>, because a missing key gives an empty value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Two Sum with a Map<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Find two numbers that add up to a target. A map gives an O(n) answer instead of checking every pair.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ twoSum returns the positions of two numbers that add up to target.\n\/\/ The last value is true when such a pair exists.\nfunc twoSum(numbers []int, target int) (int, int, bool) {\n    seen := map[int]int{} \/\/ number -&gt; its index\n\n    for index, number := range numbers {\n        partner := target - number \/\/ the number we need to reach target\n\n        \/\/ ok is true if we have already seen the partner.\n        partnerIndex, ok := seen[partner]\n        if ok {\n            return partnerIndex, index, true\n        }\n        seen[number] = index \/\/ remember this number for later\n    }\n    return 0, 0, false\n}\n\nfunc main() {\n    fmt.Println(twoSum([]int{2, 7, 11, 15}, 9))\n    fmt.Println(twoSum([]int{3, 2, 4}, 6))\n    fmt.Println(twoSum([]int{1, 2}, 10))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 1 true\n1 2 true\n0 0 false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For each number, you ask &#8220;have I already seen the partner I need?&#8221;. If yes, you return both positions. If not, you store the number in the map and move on, so the whole search is a single pass.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Find the Missing Number<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A slice holds the numbers 1 to n with one missing. The sum formula finds it without any search.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := []int{1, 2, 4, 6, 3, 7, 8}\n\n    \/\/ Step 1: the full list would have one more number.\n    count := len(numbers) + 1\n\n    \/\/ Step 2: the sum of 1 to count is count * (count + 1) \/ 2.\n    expected := count * (count + 1) \/ 2\n\n    \/\/ Step 3: add the numbers we really have.\n    actual := 0\n    for _, number := range numbers {\n        actual += number \/\/ same as actual = actual + number\n    }\n\n    \/\/ Step 4: the gap between the two sums is the missing number.\n    fmt.Println(\"Missing:\", expected-actual)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Missing: 5<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A full list of 1 to 8 adds up to 36. The numbers you have add up to 31. So the missing number is 36 &#8211; 31, which is 5.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Search a Sorted 2D Grid<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each row and column is sorted. You start at the top right and step left or down.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ searchGrid returns the row and column of target, and true if it was found.\nfunc searchGrid(grid [][]int, target int) (int, int, bool) {\n    \/\/ Start at the top right corner.\n    row := 0\n    col := len(grid[0]) - 1\n\n    for row &lt; len(grid) &amp;&amp; col &gt;= 0 {\n        current := grid[row][col]\n        if current == target {\n            return row, col, true\n        } else if current &gt; target {\n            col-- \/\/ too big, so move left to smaller numbers\n        } else {\n            row++ \/\/ too small, so move down to bigger numbers\n        }\n    }\n    return -1, -1, false\n}\n\nfunc main() {\n    grid := [][]int{\n        {1, 4, 7, 11},\n        {2, 5, 8, 12},\n        {3, 6, 9, 16},\n    }\n    fmt.Println(searchGrid(grid, 9))\n    fmt.Println(searchGrid(grid, 10))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 2 true\n-1 -1 false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the current number is too big, everything below it is bigger too, so you move left. If it is too small, you move down. Every step removes a whole row or column, so this runs in O(rows + cols) time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Binary Search on Structs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>slices.BinarySearchFunc<\/code> searches by a field when the slice is sorted by that field.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"cmp\"\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ Book is one book in a library, sorted by ID.\ntype Book struct {\n    ID    int\n    Title string\n}\n\n\/\/ compareID tells BinarySearchFunc if a book's ID is smaller, equal or bigger than id.\nfunc compareID(book Book, id int) int {\n    return cmp.Compare(book.ID, id)\n}\n\nfunc main() {\n    books := []Book{{101, \"Go Basics\"}, {205, \"Web APIs\"}, {310, \"Concurrency\"}, {420, \"Testing\"}}\n    position, found := slices.BinarySearchFunc(books, 310, compareID)\n    fmt.Println(position, found, books[position].Title)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 true Concurrency<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The books are sorted by <code>ID<\/code>, so binary search works on that field. <code>compareID<\/code> tells the search whether to go left or right. It finds ID 310 at index 2, which is &#8220;Concurrency&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Linear vs Binary Go Searching Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Choosing between Go searching programs depends on your data. The diagram and table below compare the two main methods.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-2-1024x640.png\" alt=\"Go searching programs comparison of linear search and binary search\" class=\"wp-image-761\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Linear search works on any data, while binary search needs sorted data but is far faster on large lists.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Linear search<\/th><th>Binary search<\/th><\/tr><\/thead><tbody><tr><td>Sorted data needed<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Checks for 1,000,000 items<\/td><td>Up to 1,000,000<\/td><td>About 20<\/td><\/tr><tr><td>Code risk<\/td><td>Very low<\/td><td>Off-by-one bugs<\/td><\/tr><tr><td>Works on linked data<\/td><td>Yes<\/td><td>No, needs index access<\/td><\/tr><tr><td>Go helper<\/td><td>slices.Index<\/td><td>slices.BinarySearch<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, use linear search for small or unsorted lists. Sort once and use binary search when you search a big list many times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Searching Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Binary search on unsorted data:<\/strong> it returns wrong answers without any error, so sort first.<\/li>\n\n\n<li><strong>Wrong loop condition:<\/strong> use <code>low &lt;= high<\/code>, or you can miss the last item.<\/li>\n\n\n<li><strong>Not moving past mid:<\/strong> <code>low = mid<\/code> instead of <code>mid + 1<\/code> can loop forever.<\/li>\n\n\n<li><strong>Ignoring the found flag:<\/strong> <code>slices.BinarySearch<\/code> returns an index even when the value is missing.<\/li>\n\n\n<li><strong>Searching a map in a loop:<\/strong> a map lookup <code>m[key]<\/code> is already O(1), so you do not need to loop.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Searching Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Prefer the standard library<\/strong>: <code>slices.Index<\/code>, <code>slices.BinarySearch<\/code> and <code>sort.Search<\/code>.<\/li>\n\n\n<li><strong>Keep data sorted<\/strong> if you search it often, and use binary search.<\/li>\n\n\n<li><strong>Use a map<\/strong> when you look up by key many times.<\/li>\n\n\n<li><strong>Test the edges<\/strong>: empty slice, one item, first item, last item and missing value.<\/li>\n\n\n<li><strong>Say the complexity<\/strong> of your search in interviews, before you write code.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Searching Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers ask these Go searching programs often in coding rounds. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Binary Search Questions on Go Searching Programs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. Why is binary search O(log n)?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each step cuts the search space in half. For 1,024 items you need at most 10 steps, because 2 to the power 10 is 1,024.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := []int{1, 3, 5, 7, 9, 11, 13}\n    target := 11\n    low := 0\n    high := len(numbers) - 1\n    steps := 0\n\n    for low &lt;= high {\n        steps++ \/\/ count each check\n        mid := low + (high-low)\/2\n        if numbers[mid] == target {\n            break\n        } else if numbers[mid] &lt; target {\n            low = mid + 1\n        } else {\n            high = mid - 1\n        }\n    }\n    fmt.Println(\"Steps:\", steps)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Steps: 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first middle is 7, so you move right. The next middle is 11, so the loop stops after 2 steps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What does sort.Search return when nothing matches?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"sort\"\n)\n\nfunc main() {\n    numbers := []int{2, 4, 6}\n\n    \/\/ First index where the number is 5 or more.\n    fmt.Println(sort.Search(len(numbers), func(i int) bool { return numbers[i] &gt;= 5 }))\n\n    \/\/ First index where the number is 10 or more. There is none.\n    fmt.Println(sort.Search(len(numbers), func(i int) bool { return numbers[i] &gt;= 10 }))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2\n3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It returns the first index where the rule is true. If the rule is never true, it returns <code>len(numbers)<\/code>, so always check the result before you use it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    numbers := []int{10, 20, 30}\n    fmt.Println(slices.BinarySearch(numbers, 5))  \/\/ where would 5 go?\n    fmt.Println(slices.BinarySearch(numbers, 35)) \/\/ where would 35 go?\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 false\n3 false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">5 would go at the start, and 35 would go at the end. The <code>false<\/code> tells you neither value is in the slice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Search Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What is the time complexity of two-sum with a map?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is O(n) time and O(n) memory. The brute force version that checks every pair is O(n squared) time but uses no extra memory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    stock := map[string]int{\"pen\": 40, \"bag\": 0}\n\n    \/\/ ok is true if the key exists in the map.\n    quantity, ok := stock[\"bag\"]\n    fmt.Println(quantity, ok)\n\n    quantity, ok = stock[\"cap\"]\n    fmt.Println(quantity, ok)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0 true\n0 false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both lookups return 0, so you cannot tell them apart by value alone. The <code>ok<\/code> flag shows that &#8220;bag&#8221; exists but &#8220;cap&#8221; does not.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. How do you find the missing number with XOR?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    numbers := []int{1, 2, 4, 5}\n\n    \/\/ ^ is XOR. Two equal numbers XOR to 0, and x XOR 0 is x.\n    result := 0\n\n    \/\/ Step 1: XOR every number from 1 to 5 (the full list).\n    for i := 1; i &lt;= len(numbers)+1; i++ {\n        result ^= i \/\/ same as result = result ^ i\n    }\n\n    \/\/ Step 2: XOR every number we have. Pairs cancel out.\n    for _, number := range numbers {\n        result ^= number\n    }\n    fmt.Println(result)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">XOR of a number with itself is 0. So every number that appears twice cancels out, and only the missing 3 is left. This also avoids overflow for big sums.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    words := []string{\"go\", \"rust\", \"go\", \"java\"}\n    fmt.Println(slices.Index(words, \"go\"))\n\n    \/\/ words[1:] skips the first word, so indexes start again at 0.\n    position := slices.Index(words[1:], \"go\")\n    fmt.Println(position, position+1)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Searching a sub-slice gives an index inside that sub-slice. Add the offset back to get the position in the full slice.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. Can you use binary search on a linked list?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not efficiently. Binary search needs to jump to the middle in one step, and a linked list must walk there item by item. So the total time becomes O(n).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. How do you find the peak element in a slice?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ peak returns the index of a number that is bigger than its neighbours.\nfunc peak(numbers []int) int {\n    low := 0\n    high := len(numbers) - 1\n    for low &lt; high {\n        mid := low + (high-low)\/2\n        if numbers[mid] &lt; numbers[mid+1] {\n            low = mid + 1 \/\/ the numbers still go up, so the peak is to the right\n        } else {\n            high = mid \/\/ mid could be the peak, so keep it\n        }\n    }\n    return low\n}\n\nfunc main() {\n    numbers := []int{1, 3, 8, 12, 4, 2}\n    index := peak(numbers)\n    fmt.Println(index, numbers[index])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>3 12<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A peak is bigger than its neighbours. If the next item is bigger, a peak must be to the right, so binary search still works on unsorted data here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11. When is a map better than binary search?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you look up by exact key many times and do not need order. A map gives O(1) average lookups, while binary search gives O(log n) but also supports range queries like &#8220;first price above 500&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\nfunc main() {\n    var empty []int \/\/ a nil slice with no items\n    fmt.Println(slices.Index(empty, 1), slices.Contains(empty, 1))\n\n    position, found := slices.BinarySearch(empty, 1)\n    fmt.Println(position, found)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>-1 false\n0 false<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All search helpers work safely on an empty or nil slice. They simply report that nothing was found.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This task turns maths into one of the Go searching programs. Write a program that finds the square root of a number rounded down, such as 17 giving 4, using binary search instead of <code>math.Sqrt<\/code>. Hint: search between 0 and n for the largest <code>mid<\/code> where <code>mid*mid &lt;= n<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/slices#BinarySearch\">slices package: BinarySearch<\/a><\/li>\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/sort#Search\">sort package: Search<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/tour\/moretypes\/19\">A Tour of Go: Maps<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go searching programs use linear search for any data and binary search for sorted data.<\/li>\n\n\n<li>Binary search halves the work each step, so it runs in O(log n).<\/li>\n\n\n<li><code>slices.BinarySearch<\/code> returns the insert position even when the value is missing.<\/li>\n\n\n<li>A map lookup with the <code>ok<\/code> flag is the fastest way to search by key.<\/li>\n\n\n<li>Test empty slices, the first and last item and missing values every time.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we look closely at Go&#8217;s key-value collection. Read <a href=\"https:\/\/smitabharat.com\/blog\/go-map-programs\/\">Go map programs with output<\/a>, with word counts, sorting maps and grouping. For the theory first, see <a href=\"https:\/\/smitabharat.com\/blog\/go-maps-examples\/\">Go maps explained with examples<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go searching programs find a value in a collection, such as an order ID in a list or a word in a dictionary. In&hellip;<\/p>\n","protected":false},"author":1,"featured_media":762,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,104,115],"swapz_series":[],"class_list":["post-780","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-programs","tag-go-searching"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Searching Programs: Linear and Binary Search<\/title>\n<meta name=\"description\" content=\"Go searching programs with output: linear and binary search, first and last position, rotated array, two sum, plus search interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-searching-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Searching Programs: Linear and Binary Search\" \/>\n<meta property=\"og:description\" content=\"Go searching programs with output: linear and binary search, first and last position, rotated array, two sum, plus search interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-searching-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-06T09:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Searching Programs: Linear Search, Binary Search, Two Sum\",\"datePublished\":\"2026-10-06T09:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/\"},\"wordCount\":1967,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-searching-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Programs\",\"Go Searching\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/\",\"name\":\"Go Searching Programs: Linear and Binary Search\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-searching-programs-featured.png\",\"datePublished\":\"2026-10-06T09:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:36+00:00\",\"description\":\"Go searching programs with output: linear and binary search, first and last position, rotated array, two sum, plus search interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-searching-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-searching-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Searching Programs featured image: linear and binary search output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-searching-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Searching Programs: Linear Search, Binary Search, Two Sum\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Searching Programs: Linear and Binary Search","description":"Go searching programs with output: linear and binary search, first and last position, rotated array, two sum, plus search interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Searching Programs: Linear and Binary Search","og_description":"Go searching programs with output: linear and binary search, first and last position, rotated array, two sum, plus search interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-06T09:30:00+00:00","article_modified_time":"2026-11-28T19:59:36+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Searching Programs: Linear Search, Binary Search, Two Sum","datePublished":"2026-10-06T09:30:00+00:00","dateModified":"2026-11-28T19:59:36+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/"},"wordCount":1967,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Programs","Go Searching"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/","name":"Go Searching Programs: Linear and Binary Search","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-featured.png","datePublished":"2026-10-06T09:30:00+00:00","dateModified":"2026-11-28T19:59:36+00:00","description":"Go searching programs with output: linear and binary search, first and last position, rotated array, two sum, plus search interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-searching-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-searching-programs-featured.png","width":2400,"height":1260,"caption":"Go Searching Programs featured image: linear and binary search output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-searching-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Searching Programs: Linear Search, Binary Search, Two Sum"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=780"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/780\/revisions"}],"predecessor-version":[{"id":882,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/780\/revisions\/882"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/762"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=780"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}