Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid argument (type interface {}) for len and index #48724

Closed
antzjm opened this issue Oct 1, 2021 · 2 comments
Closed

invalid argument (type interface {}) for len and index #48724

antzjm opened this issue Oct 1, 2021 · 2 comments

Comments

@antzjm
Copy link

antzjm commented Oct 1, 2021

What version of Go are you using (go version)?

go 1.15.15

Does this issue reproduce with the latest release?

When I defined a function which use interface{} parameter, I meet some go build error with my env.
The error information is that invalid argument v (type interface {}) for len and v[0] (type interface {} does not support indexing).

Here is the function I defined

func writeResourcesAsJson(resources interface{}, resourceType string,indent string, out io.Writer) error {
	switch v := resources.(type) {
	case []models.Vi,[]models.CnfPackage,[]models.Dpp:
		if len(v) == 0 {
			_, err := out.Write([]byte(fmt.Sprintf("No %s(s) found.\n", resourceType)))
			return err
		}
	
		var data []byte
	
		var err error
	
		if len(v) == 1 {
			data, err = json.MarshalIndent(v[0], "", indent)
			if err != nil {
				return err
			}
		} else {
			data, err = json.MarshalIndent(&v, "", indent)
			if err != nil {
				return err
			}
		}
		data = append(data, []byte("\n")...)
		_, err = out.Write(data)

		return err
	default:
		return errors.New("Resource type error: only support write vi, cnfpackage, dpp as json format")
	}

	return nil

}

Besides, I also have write some test code and running ok. Here is my test code

package main

import (
    "fmt"
)


type ant string

func test(b interface{}){
    switch v := b.(type) {
    case []ant:
        l := len(v)
        fmt.Println(l)
        fmt.Printf("%T   %s\n",v,v)
    default:
        fmt.Println("b1.(type):", "other", v)
    }
}


func main() {
    b1 := []ant{"1","2","3"}
    test(b1)
}

I am not very clear what is difference between this two code. So I want to ask help for golang support team. Very tks for your comment. :)

@cherrymui
Copy link
Member

See https://golang.org/ref/spec#Switch_statements, "Type switches" section, especially

In clauses with a case listing exactly one type, the variable has that type; otherwise, the variable has the type of the expression in the TypeSwitchGuard.

and the examples below.

That's where it differs in your two examples.

@antzjm
Copy link
Author

antzjm commented Oct 2, 2021

@cherrymui Tks for your comments.

@golang golang locked and limited conversation to collaborators Oct 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants