返回接口时的类型断言

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

Type assertion when returning an interface

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对golang还不熟悉,但根据我目前的了解,值类型引用类型都可以实现一个接口。但是在类型断言方面,返回一个结构体似乎很重要。请看下面的代码:

package main

import (
	"fmt"
)

type SomeError interface {
	Error() string
}

type ConcreteError struct{}

func (ConcreteError) Error() string {
	return "?"
}

func returnPointer() SomeError {
	return &ConcreteError{}
}

func returnVal() SomeError {
	return ConcreteError{}
}

func main() {
	pointer := returnPointer()
	value := returnVal()
	_, pointerWithPointer := pointer.(*ConcreteError)
	_, pointerWithValue := pointer.(ConcreteError)

	_, valueWithValue := value.(ConcreteError)
	_, valueWithPointer := value.(*ConcreteError)

	fmt.Printf("返回一个指针,使用(*ConcreteError)进行断言:%v\n", pointerWithPointer) // true
	fmt.Printf("返回一个指针,使用(ConcreteError)进行断言:%v\n", pointerWithValue) // false
	fmt.Printf("返回一个值,使用(ConcreteError)进行断言:%v\n", valueWithValue) // true
	fmt.Printf("返回一个值,使用(*ConcreteError)进行断言:%v\n", valueWithPointer) // false
}

所以,如果我的理解是正确的,用户需要知道结构体是如何返回的,才能正确断言其类型?

我猜测并假设在golang中的标准做法是始终返回一个指向结构体的指针(例如像*PathError一样)。

播放链接:这里

英文:

I'm new to golang; however based on my current knowledge I understand that a value-type and a reference-type can both fulfill an interface. But it seems in regards to type assertion, how you return a struct does matter. See the following:

package main

import (
	"fmt"
)

type SomeError interface {
	Error() string
}

type ConcreteError struct{}

func (ConcreteError) Error() string {
	return "?"
}

func returnPointer() SomeError {
	return &ConcreteError{}
}

func returnVal() SomeError {
	return ConcreteError{}
}

func main() {
	pointer := returnPointer()
	value := returnVal()
	_, pointerWithPointer := pointer.(*ConcreteError);
	_, pointerWithValue := pointer.(ConcreteError);
	
	_, valueWithValue := value.(ConcreteError);
	_, valueWithPointer := value.(*ConcreteError)
	
	fmt.Printf("Returning a pointer, assert using (*ConcreteError): %v\n", pointerWithPointer); // true
	fmt.Printf("Returning a pointer, assert using (ConcreteError): %v\n", pointerWithValue); // false
	fmt.Printf("Returning a value,   assert using (ConcreteError): %v\n", valueWithValue); // true
	fmt.Printf("Returning a value,   assert using (*ConcreteError): %v\n", valueWithPointer); // false
}

So if my understanding is correct, the user needs to know how the struct is returned to correctly assert its type?

I'm going to guess and assume the standard practice in golang is to always return a pointer to a struct(i.e like *PathError)?

link to play: here

答案1

得分: 1

如果我的理解正确,用户需要知道结构体是如何返回的,以正确断言其类型?

这取决于情况。如果你需要类型断言通过,你肯定需要知道值的确切类型。a*a 是不同的类型。

我猜测并假设在 Go 语言中的标准做法是始终返回一个指向结构体的指针(例如像 *PathError 这样)吗?

不是的。

英文:

> So if my understanding is correct, the user needs to know how the struct is returned to correctly assert its type?

It depends. If you need the type assertion to pass - you surely need to know the exact type of the value. a and *a are different types.

> I'm going to guess and assume the standard practice in golang is to always return a pointer to a struct(i.e like *PathError)?

No.

huangapple
  • 本文由 发表于 2017年5月3日 10:10:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/43749952.html
匿名

发表评论

匿名网友

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

确定