gpt4 book ai didi

go - 'sconn, ok:= conn.(*tls.Conn)' 是什么意思?

转载 作者:行者123 更新时间:2023-12-01 22:44:21 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Is this casting in golang?

(1 个回答)



What does "i.(string)" actually mean in golang syntax? [duplicate]

(2 个回答)



What is the meaning of "dot parenthesis" syntax? [duplicate]

(1 个回答)



What does "r.(flate.Reader)" mean in golang's zlib/reader.go file?

(1 个回答)



What is this "err.(*exec.ExitError)" thing in Go code?

(2 个回答)


2年前关闭。




我在某个人的代码中遇到了一条语句 ** sconn, ok:= conn.(*tls.Conn) **。
我想这是某种类型的转换? conn 是 net.Conn 的类型。这可以将 conn 转换为 *tls.Conn

我不确定这是什么意思。这个语法是什么意思?

最佳答案

它试图断言 conn (大概有一个接口(interface)类型)是 *tls.Conn (可能是满足接口(interface)的类型)并将(如果是接口(interface)类型值的基础值)分配(现在 *tls.Conn)给 sconn .如果它是正确的底层类型,ok变量将包含 bool 值 true , 否则 bool 值 false .

https://golang.org/ref/spec#Type_assertions

关于go - 'sconn, ok:= conn.(*tls.Conn)' 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61630993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com