doumubi6784 2018-10-24 09:23
浏览 379

golang中是否有任何函数会发现值是否在数组中?

Suppose there is an slice having integers in it. And we have declared a variable which contains a integer value then I have to find the value from that slice without using for loop.

Using for loop I do like this:-

package main

import (
   "fmt"
)

func main() {
  value := 10
  var interf []interface{}
  for i := 1; i <= value; i++{
    interf = append(interf, i)
  } 
  fmt.Println(interf)
  for _,v := range interf{
    if value == v{
       fmt.Println("Matched")
    }
  }
}

How we do this same thing without using for loop

  • 写回答

1条回答 默认 最新

  • dpz90118 2018-10-24 09:36
    关注

    Without a for loop, no* (see How to search for an element in a golang slice), but... what do you have against for loops? Why can't you use loops for this? This question is like "can I do it without coding"?

    * Actually you could do it without a for loop using a recursive function, but that is good only for educational purposes, and is worthless for practical purposes. See solution at the end of the answer.

    There is no ready function for this in the standard library, but this is how easy it is to create one yourself:

    func find(what interface{}, where []interface{}) (idx int) {
        for i, v := range where {
            if v == what {
                return i
            }
        }
        return -1
    }
    

    And using it:

    what := 10
    where := []interface{}{1, 2, 3, 10, 5}
    fmt.Println(find(what, where))
    

    Output (try it on the Go Playground):

    3
    

    Also note that it would be faster and more convenient to use []int slice type instead of []interface{}:

    func find(what int, where []int) (idx int) {
        for i, v := range where {
            if v == what {
                return i
            }
        }
        return -1
    }
    

    And then using it:

    what := 10
    where := []int{1, 2, 3, 10, 5}
    fmt.Println(find(what, where))
    

    Output is the same. Try this one on the Go Playground.

    You could create a function that accepts slices of any type using the interface{} type, but that would require reflection to implement it, which would be slower and not worthy to be used. Instead just create a function with concrete slice types if you need, or just use the for loop in place.

    For completeness, here's the solution that uses no for loops but a recursive function. This is here only for educational purposes, the solutions above are superior to this:

    func find(what int, where []int) (idx int) {
        if len(where) == 0 {
            return -1
        }
        if what == where[0] {
            return 0
        }
        if idx = find(what, where[1:]); idx < 0 {
            return -1 // Not found in the rest of the slice
        }
        return 1 + idx
    }
    

    Try this one on the Go Playground.

    评论

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗