.(data_type) 方法究竟是做什么的? [英] What exactly does .(data_type) method called/do?

查看:29
本文介绍了.(data_type) 方法究竟是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一段使用 .(string) 方法的代码.不知道这叫什么,我很难找到它.

I came a cross a piece of code that used .(string) method. Not knowing what this is called I had difficulties searching for it.

这是我试图理解它:

package main

import "fmt"
import "reflect"

func main(){
    var b interface{}
    b = "silly"

    fmt.Println(reflect.TypeOf(b.(string))) // we know that b 
                                            // is a string                     
                                            // at compile time

    fmt.Println(reflect.TypeOf(b))          // we do not

}

结果:

string
string

然而,我认为 reflect.TypeOf 发生在运行时,而 .(string) 会告诉编译器 b 是确实是一个字符串,这可以用来告诉编译器一个变量是某种类型.我的理解对吗?

However, I think that reflect.TypeOf takes place at run time, while .(string) would tell the compiler that b is indeed a string, and this could be used to tell the compiler that a variable is of certain type. Is my understanding right?

goplayground

推荐答案

b.(string) 被称为 类型断言.如Effective Go 所述:

b.(string) is called a type assertion. As written in Effective Go:

类型断言采用接口值并从中提取指定显式类型的值.

A type assertion takes an interface value and extracts from it a value of the specified explicit type.

所以,是的,您从类型断言中获得的值不是接口值,而是显式类型.您还可以通过添加一个无类型的布尔值来测试类型断言是否成功:

So, yes, the value you get from a type assertion is not an interface value, but is of the explicit type. You can also test if the type assertion was successful by adding an untyped boolean value:

s, ok := b.(string) // s is of type string
if !ok {
    // b did not contain a value of type string!
}

进一步解释以消除任何可能的误解:

To explain further to clear out any possible misunderstanding:

类型断言不会像您建议的那样告诉 Go b 是一个字符串".它的作用是,在运行时,它会尝试从 b 中提取一个字符串,如果 b 包含其他类型(除非分配可选的 bool 值),则它会恐慌.

A type assertion doesn't "tell Go that b is a string" as you suggested. What it does is that it will, in run time, try to extract a string from b, and panic if b contains some other type (unless assigning the optional bool value).

您从断言中获得的值确实是 string 类型,允许您执行切片(您不能对接口值进行切片)或检查其 len.

The value that you get from the assertion will indeed be of type string, allowing you to do things like slicing (you cannot slice an interface value) or checking its len.

这篇关于.(data_type) 方法究竟是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆