# Extracting numbers in specific ranges from a slice

Consider a slice with some floating point numbers:

nums := []float64{0.17898, 0.25512, 1.98123, 1.35902, 0.97642}


Suppose I want to extract the numbers lying in between the range from 1 to 1.99 and ones lying in between the range from 0.0 to 0.99 (inclusive of both limits) from this slice and append it into two new separate slices each, then what would be the best possible way to do so?

I have the following code:

package main

import (
"fmt"
)

func main() {
nums := []float64{0.17, 0.25, 2.19, 0.98, 1.35, 5.97, 1.21}
var oneSlice, zeroSlice []float64
for _, num := range nums {
if num <= 1.99 && num >= 1.0 {
oneSlice = append(oneSlice, num)
} else if num <= 0.99 && num >= 0.0 {
zeroSlice = append(zeroSlice, num)
}
}
fmt.Print("1.0-1.99 values:", oneSlice, "\n0.0-0.99 values:", zeroSlice)
}


1.0-1.99 values:[1.35 1.21]
0.0-0.99 values:[0.17 0.25 0.98]


Are there any better approaches than this? (such as avoiding the if & else, or a function that can be used, which I'm not aware of? - for e.g.: a function which can compare slice elements with a given value or range of values?)

I'm new to Go, so am just wondering if there could be a possibly better approach to this! Any help is appreciated :)

• Welcome to Code Review where we review working code from your project and make suggestions on how to improve that code. Hypothetical code is considered off-topic and the question may be closed by the community. The question currently appears to be too hypothetical. If you reword the question so that it doesn't appear to be hypothetical and put real working code in the question this will become a good question. Take a look at our guidelines for asking a good question. You have a good start already. – pacmaninbw Oct 11 '20 at 15:21
• @pacmaninbw I was able to run the code online and see output that, while not declared explicitly in the post, matched the description. I am going to update the wording in the description here – Sᴀᴍ Onᴇᴌᴀ Oct 12 '20 at 15:40
• @SᴀᴍOnᴇᴌᴀ Thanks for commenting that. In both ways, I thought the question was self-explanatory with the wording, so I didn't see a reason to edit it. (also, I obviously wouldn't throw in code that doesn't work!) – Anirban166 Oct 13 '20 at 7:30