无法在非接口值上进行类型切换。

huangapple go评论102阅读模式
英文:

Cannot type switch on non-interface value

问题

我正在使用以下虚拟代码进行类型断言的测试,并且遇到了错误:

> 无法对非接口值进行类型切换

有人知道这是什么意思吗?

package main

import "fmt"
import "strconv"

type Stringer interface {
    String() string
}

type Number struct {
    v int
}

func (number *Number) String() string {
    return strconv.Itoa(number.v)
}

func main() {
    n := &Number{1}
    switch v := n.(type) {
    case Stringer:
        fmt.Println("Stringer:", v)
    default:
        fmt.Println("Unknown")
    }
}

链接:http://play.golang.org/p/Ti4FG0m1mc

英文:

I am playing with type assertion using the following dummy code, and I got the error:

> cannot type switch on non-interface value

Does anyone know what does that mean?

package main

import "fmt"
import "strconv"

type Stringer interface {
	String() string
}

type Number struct {
	v int
}

func (number *Number) String() string {
	return strconv.Itoa(number.v)
}

func main() {
	n := &Number{1}
	switch v := n.(type) {
	case Stringer:
		fmt.Println("Stringer:", v)
	default:
		fmt.Println("Unknown")
	}
}

http://play.golang.org/p/Ti4FG0m1mc

答案1

得分: 79

我找到了答案,就是在类型断言之前将 n 转换为 interface{}

switch v := interface{}(n).(type)
英文:

I figured out the answer, which is to cast n to interface{} before the type assertion:

switch v := interface{}(n).(type)

答案2

得分: 39

类型切换需要一个接口来进行内省。如果你将一个已知类型的值传递给它,它会出错。如果你创建一个接受接口作为参数的函数,它将正常工作:

func typeSwitch(tst interface{}) {
    switch v := tst.(type) {
        case Stringer:
            fmt.Println("Stringer:", v)
        default:
            fmt.Println("Unknown")
    }
}

在这里可以查看完整的代码:http://play.golang.org/p/QNyf0eG71_,以及关于接口的Go语言文档:http://golang.org/doc/effective_go.html#interfaces。

英文:

Type switches require an interface to introspect. If you are passing a value of known type to it it bombs out. If you make a function that accepts an interface as a parameter, it will work:

func typeSwitch(tst interface{}) {
    switch v := tst.(type) {
        case Stringer:
	       fmt.Println("Stringer:", v)
        default:
	       fmt.Println("Unknown")
    }
}

See the full code here http://play.golang.org/p/QNyf0eG71_ and the golang documentation on interfaces http://golang.org/doc/effective_go.html#interfaces.

答案3

得分: 2

有两种类型转换:

  1. 基本数据类型之间的转换。对于这种情况,我们可以使用直接转换。

    i := 48

    str := string(i)

  2. 但是使用 value.(type) 进行类型转换是用于在类层次结构之间进行转换的(例如,我们想要从接口中获取特定的实现)。否则,会出现上述错误。

英文:

There are two kinds of type conversions

  1. conversion between basic data types. For this we can use the direct casting

    i := 48

    str := string(i)

  2. But type conversion using value.(type) is for conversion among the class hierarchy, (e.g. where we want to get the specific implementation from the interface). Else, we get the above error.

答案4

得分: 0

在Go语言中没有类型转换,你正在进行类型转换操作。

英文:

There are no typecasts in Go. You are doing a type conversion.

huangapple
  • 本文由 发表于 2014年4月19日 23:59:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/23172219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定