类型转换与具体结构体上的类型断言有什么区别?

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

Type cast vs type assertion on concrete struct?

问题

我对golang还不熟悉,所以如果这个问题太幼稚,请原谅。我找了一下,但找不到对我基本问题的答案。

假设我有一个具体的结构体和如下所示的方法。

type MyData struct{
    field1 string
    field2 int 
}

func(a MyData) OperatorOnString() string{
    return a.field1.(string)
}

func(a MyData) OperatorOnInt() int{
    return a.field2.(int)
}

我的问题是,我可以进行类型转换并返回,而不是进行断言吗?根据我目前所学,断言是用于接口类型的数据。但在这种情况下,我有具体的类型。我是否仍应使用断言,或者我可以像return int(a.field2)这样做。我知道这个例子很简单,但我困惑的是何时使用这两种转换类型。或者这里是否涉及一些golang的惯用法?

谢谢

英文:

I am new to golang, so appologize if this question is too naive. Looked around, but could not find answer to my basic question.

Lets say I have a concrete struct and methods as shown below.

 type MyData struct{
     field1 string
     field2 int 
     }

func(a MyData) OperatorOnString() string{
  return a.field1.(string)
}

func(a MyData) OperatorOnInt() int{
  return a.field2.(int)
}

My question is, can I type cast and return rather than performing assertion? From what I have learned so far is that, assertion is used on data of type interface. But in this case I have concrete type. Should I still use assertion or I can do something like return int(a.field2). I know this example is trivial, but the point that I am confused is when to use between the two conversion types. Or is there some golang idiomaticity involved here?

Thanks

答案1

得分: 13

首先,类型断言只能用于接口类型:

> 对于一个接口类型的表达式 x 和一个类型 T,主表达式

x.(T)

> 断言 x 不是 nil,并且存储在 x 中的值是类型 Tx.(T) 的表示法被称为类型断言。

但是你正在将其应用于非接口类型的字段(intstring)。这会使编译器不开心

其次,如果你想从一个方法/函数返回类型 T,只需要返回一个类型为 T 的表达式,而你的字段恰好就是这种类型。正确的代码就很简单:

package main

import "fmt"

type MyData struct {
        field1 string
        field2 int
}

func (a MyData) OperatorOnString() string {
        return a.field1
}

func (a MyData) OperatorOnInt() int {
        return a.field2
}

func main() {
        a := MyData{"foo", 42}
        fmt.Println(a.OperatorOnString(), a.OperatorOnInt())
}

Playground


输出:

foo 42
英文:

First of all, type assertion can be used only on interfaces:

> For an expression x of interface type and a type T, the primary expression

x.(T)

> asserts that x is not nil and that the value stored in x is of type T. The notation x.(T) is called a type assertion.

But you're applying it to non interface typed fields (int and string). That makes compiler unhappy.

Secondly, if you want to return type T from a method/function, it's always enough to return an expression of type T, which your fields already happen to be. The correct code is then easy:

package main

import "fmt"

type MyData struct {
        field1 string
        field2 int
}

func (a MyData) OperatorOnString() string {
        return a.field1
}

func (a MyData) OperatorOnInt() int {
        return a.field2
}

func main() {
        a := MyData{"foo", 42}
        fmt.Println(a.OperatorOnString(), a.OperatorOnInt())
}

Playground


Output:

foo 42

huangapple
  • 本文由 发表于 2013年5月8日 21:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/16442053.html
匿名

发表评论

匿名网友

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

确定