10

Golang 类型断言的分类和例子

 3 years ago
source link: https://www.sulinehk.com/post/categories-and-examples-of-type-assertion-in-golang/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

因为对表达式断言和赋值断言的理解不够清晰,所以写了这篇博客来加深理解。

Golang Logo

为了更好地理解类型断言的例子,后面的内容都遵循以下惯例:

  • 大写 I:表示接口类型;小写 i:表示接口值。
  • 大写 S:表示具体类型;小写 s:表示具体类型值。
  • 大写 T:表示具体类型和接口类型其中一种;小写 t:表示具体类型值和接口值其中一种。

表达式断言

表达式断言的语法为:i.(T),其中 i 为接口值,T 为具体类型或接口类型。发生错误时,为运行时错误。

当 T 为具体类型时,作用是把接口值转换为具体类型值

当 i 的具体类型是 T 时,断言成功:

type I interface{}
type S struct{}
var i I = S{}
_ = i.(S)
// i 的具体类型是 S,所以断言成功。

表达式返回一个新值,该值为具体类型值,类型为 T。

当 i 的具体类型不是 T 时,断言失败:

type I interface{}
type S struct{}
var i I
_ = i.(S)
// i 的具体类型未声明,不是 S,所以断言失败。

当 T 为接口类型时,作用是转换接口类型

当 i 的具体类型实现了 T 接口时,断言成功:

type I1 interface{}
type I2 interface{}
type S struct{}
var i I1 = S{}
_ = i.(I2)
// i 的具体类型 S 实现了 I2 接口,所以断言成功。i 的接口类型从 I1 变为 I2。

表达式返回一个新值,该值为接口值,类型为 T。接口值里面的具体类型和具体值不变。

当 i 的具体类型未实现 T 接口时,断言失败:

type I1 interface{}
type I2 interface{ M() }
type S struct{}
var i I1 = S{}
_ = i.(I2)
// i 的具体类型 S 没有实现 M() 方法,即没实现 I2 接口,所以断言失败。

赋值断言的语法为:i = t,其中 i 为接口值,t 为具体类型值或接口值。发生错误时,为编译时错误。

当 t 为具体类型值时,作用是转换具体类型

当 t 的具体类型实现了 i 的接口时,断言成功:

type I interface{}
type S1 struct{}
type S2 struct{}
t := S1{}
var i I = S2{}
i = t
// t 的具体类型 S1 实现了 I 接口,所以断言成功。i 的具体类型从 S2 变为 S1。

i 的具体类型变为 t 的具体类型,接口类型保持不变。

当 t 的具体类型未实现 i 的接口时,断言失败:

type I interface{ M() }
type S1 struct{}
type S2 struct{}
func (S2) M() {}

t := S1{}
var i I = S2{}
i = t
// t 的具体类型 S1 没有实现 M() 方法,即没实现 I 接口,所以断言失败。

当 t 为接口值时,作用是转换接口类型

当 t 的具体类型实现了 i 的接口时,断言成功:

type I1 interface{}
type I2 interface{}
type S1 struct{}
type S2 struct{}
var t I1 = S1{}
var i I2 = S2{}
i = t
// t 的具体类型 S1 实现了 i 的接口 I2,i 的接口类型从 I2 变为 I1。

i 的接口类型变为 t 的接口类型,具体类型保持不变。显示转换也可以达到同样的作用,如:i = io.Writer(t)

当 t 的具体类型未实现 i 的接口时,断言失败:

type I1 interface{}
type I2 interface{ M() }
type S1 struct{}
type S2 struct{}
func (S2) M() {}

var t I1 = S1{}
var i I2 = S2{}
i = t
// t 的具体类型 S1 未实现 i 的接口 I2,所以断言失败。

Recommend

  • 31
    • studygolang.com 6 years ago
    • Cache

    golang: 类型转换和类型断言

    类型转换在程序设计中都是不可避免的问题。当然有一些语言将这个过程给模糊了,大多数时候开发者并不需要去关注这方面的问题。但是golang中的类型匹配是很严格的,不同的类型之间通常需要手动转换,编译器不会代你去做这个事。我之所以说...

  • 53

    通过类型断言(type assertion)方式来判断接口的具体类型, Sometimes, you may want to know the exact type of an interface variable. In this scenario, you can use type assertion : x.(T)

  • 26
    • studygolang.com 5 years ago
    • Cache

    类型转换和断言

    Golang不支持隐式类型转换 普通类型转换 普通类型转换可以转换不同但是相互兼容的类型,例如int与float,int与rune,但是string与数字类型是不能进行普通类型转换的。 例如: a = 123 x := i...

  • 44
    • Github github.com 5 years ago
    • Cache

    谈谈 TypeScript 中的类型断言

    类型断言 类型断言(Type Assertion)可以用来手动指定一个值的类型。 语法 值 as 类型 或 <类型>值 在 tsx 语法(React 的 jsx 语法的 ts 版)中必须使用前者,即

  • 19

    Hi,大家好,我是明哥。 在自己学习 Golang 的这段时间里,我写了详细的学习笔记放在我的个人微信公众号 《Go编程时光》,对于 Go 语言,我也算是个初学者,因此写的东西应该会比较适合刚接触的同学,如果你也是刚学习 Go 语言,不...

  • 38
    • studygolang.com 4 years ago
    • Cache

    Go之断言类型(assert type)

    1.背景介绍: 笔者最近在使用一个数据结构实现多个接口,但是却发现想要在一段时间内转换成接口1,另外一段时间转换成接口2,所以也就使用到了断言类型。在看过断言类型之后,觉得还是很有必要讲一讲这个断言类型,于是...

  • 19
    • studygolang.com 4 years ago
    • Cache

    聊聊golang的类型断言

    序 本文主要研究一下golang的类型断言 类型断言 x.(T) 断言x不为nil且x为T类型 如果T不是接口类型,则该断言x为T类型 如果T类接口类型,则该断言x实现了T接口

  • 10
    • studygolang.com 3 years ago
    • Cache

    golang类型转换与断言

    golang类型转换与断言 13641329565 · 1天之前 · 100 次点击 · 预计阅读时间 1 分钟 · 大...

  • 8
    • studygolang.com 2 years ago
    • Cache

    Golang中的类型和类型断言

    Golang中的类型和类型断言 LeeYubo · 2019-04-26 17:14:08 · 8268 次点击...

  • 7
    • zhangyiming748.github.io 1 year ago
    • Cache

    golang interface 的类型断言

    golang interface 的类型断言 2023-11-07 Golang ...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK