Skip to content
Related Articles

Related Articles

Searching an element of float64 type in Golang slice
  • Last Updated : 05 Sep, 2019

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice.
In the Go slice, you can search an element of float64 type in the given slice of float64s with the help of SearchFloat64s() function. This function searches for the given element in a sorted slice of float64s and returns the index of that element if present in the given slice. And if the given element is not available in the slice(it could be len(s_slice)), then it returns the index to insert the element in the slice. The specified slice must be sorted in ascending order. It is defined under the sort package so, you have to import sort package in your program for accessing SearchFloat64s function.

Syntax:

func SearchFloat64s(s_slice []float64, f float64) int

Example 1:




// Go program to illustrate how to search an 
// element float64 type in the slice of float64s
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing
    // slice of Float64s
    // Using shorthand declaration
    slice_1 := []float64{45.667, 34545.5, 655.45,
                        768.8, 79.1, 10.2, 34.67}
      
    slice_2 := []float64{56.78, 67.89, 10.7,
                         345.6, 89.4, 45.8}
  
    var f1, f2, f3 float64
    f1 = 34545.5
    f2 = 67.89
    f3 = 10.7
  
    // Sorting the given slice of Float64s
    sort.Float64s(slice_1)
    sort.Float64s(slice_2)
  
    // Displaying the slices
    fmt.Println("Slice 1: ", slice_1)
    fmt.Println("Slice 2: ", slice_2)
  
    // Searching an element of float64
    // in the given slice
    // Using SearchFloat64s function
    res1 := sort.SearchFloat64s(slice_1, f1)
    res2 := sort.SearchFloat64s(slice_2, f2)
    res3 := sort.SearchFloat64s(slice_2, f3)
  
    // Displaying the results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
  
}

Output:

Slice 1:  [10.2 34.67 45.667 79.1 655.45 768.8 34545.5]
Slice 2:  [10.7 45.8 56.78 67.89 89.4 345.6]
Result 1:  6
Result 2:  3
Result 3:  0

Example 2:




// Go program to illustrate how to search an
// element of float64 type in the slice of
// float64s
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and searching an element
    // in the given slice of float64s
    // Using SearchFloat64s function
    res1 := sort.SearchFloat64s([]float64{23.4,
                        56.7, 90.7}, 56.7)
  
    res2 := sort.SearchFloat64s([]float64{10.2,
               13.90, 25.78, 38.90}, 10.2)
  
    // Displaying the results
    fmt.Println("Result 1: ", res1)
    fmt.Println("Result 2: ", res2)
  
}

Output:

Result 1:  1
Result 2:  0



My Personal Notes arrow_drop_up
Recommended Articles
Page :