How to check if interface{} is a slice

后端 未结 3 1365
离开以前
离开以前 2021-01-12 03:49

I\'m noob in Go :) so my question may be stupid, but can\'t find answer, so.

I need a function:

func name (v interface{}) {
    if is_slice() {
              


        
3条回答
  •  庸人自扰
    2021-01-12 04:16

    icza's answer is correct, but is not recommended by go creators:

    interface{} says nothing

    A better approach may be to define a function for each type you have:

    func name(v MyInterface) {
        // do something
    }
    
    func names(vs []MyInterface) {
        for _, v := range(vs) {
            name(v)
        }
    }
    

提交回复
热议问题