Home > Backend Development > Golang > Why Does a Type Switch with Multiple Cases in Go Result in an 'undefined' Method Error?

Why Does a Type Switch with Multiple Cases in Go Result in an 'undefined' Method Error?

Patricia Arquette
Release: 2024-12-18 14:42:09
Original
137 people have browsed it

Why Does a Type Switch with Multiple Cases in Go Result in an

Type Switch with Multiple Cases in Go

In Go, a type switch allows you to match a value against multiple types. However, when using multiple cases in a type switch, understanding variable type assignments becomes crucial.

Consider the following code snippet:

package main

import (
    "fmt"
)

type A struct {
    a int
}

func (this *A) test() {
    fmt.Println(this)
}

type B struct {
    A
}

func main() {
    var foo interface{}
    foo = A{}
    switch a := foo.(type) {
        case B, A:
            a.test()
    }
}
Copy after login

When executing this code, an error is raised: a.test undefined (type interface {} is interface with no methods). This error occurs because the variable a retains the type interface{}, despite the type switch.

To understand this behavior, we need to refer to the Go spec:

"The TypeSwitchGuard may include a short variable declaration. When that form is used, the variable is declared at the beginning of the implicit block in each clause. 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."

In our case, since multiple types (B and A) are specified in the case clause, the variable a keeps the type of the expression in the TypeSwitchGuard, which is interface{}. This means that the compiler will not allow us to call the test() method on a because interface{} doesn't have a test() method.

To resolve this, we can use type assertions, which allow us to assert that a value has a specific type. Here's an updated version of the code that uses type assertions:

package main

import (
    "fmt"
)

type A struct {
    a int
}

func (this *A) test() {
    fmt.Println(this)
}

type B struct {
    A
}

func main() {
    var foo interface{}
    foo = &B{}
    if a, ok := foo.(tester); ok {
        fmt.Println("foo has test() method")
        a.test()
    }
}
Copy after login

The above is the detailed content of Why Does a Type Switch with Multiple Cases in Go Result in an 'undefined' Method Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template