You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. :)
The text was updated successfully, but these errors were encountered:
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.
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
Besides, I also have write some test code and running ok. Here is my test code
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. :)
The text was updated successfully, but these errors were encountered: