Golang有没有类似C++的decltype?
Does Golang have something like C++'s decltype?
C++ 有 decltype(expr)
。您可以声明某个其他表达式类型的对象。例如:
decltype('c') a[4]
将声明一个包含 4 个字符的数组。这是一个玩具示例,但此功能可能很有用。这是 UDP 服务器的一些 Go 代码:
conn, err := net.ListenUDP("udp", udp_addr)
...
defer conn.Close()
...
_, err = conn.WriteToUDP(data, addr)
重要的是我知道我可以做 与(的类型)结果函数(在这种情况下,有连接,ListenUDP
), 但是不知道这个是什么.在这里,因为 Go 的类型推断,我不需要知道。但是如果我想创建 5 个连接,那么我想要一个包含 5 个“ListenUDP
的结果”的数组。我做不到。我得到的最接近的是:
ret_type := reflect.TypeOf(net.DialUDP)
first_param_type := reflect.TypeOf(ret_type.Out(0))
my_arr := reflect.ArrayOf(4, first_param_type)
my_arr[0] = nil
但是最后一行不起作用。有没有办法在 Go 中做到这一点?
Go 没有 compile-time 等同于 C++ 的 decltype
。
但 Go 是一种静态类型语言:即使在短变量声明的情况下存在类型推断,类型在编译时也是已知的。 net.ListenUDP()
are not visible in the source code, but you can look it up just as easily, e.g. it takes 2 seconds to hover over it with your mouse and your IDE will display the signature. Or check online 的结果类型。或者在终端中 运行 go doc net.ListenUDP
。
net.ListenUDP()
的签名是:
func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error)
所以保存 5 个返回连接的数组类型是 [5]*net.UDPConn
。另请注意,在 Go 中使用 slices instead of arrays 更好更容易。
所以我建议使用切片类型 []*net.UDPConn
。如果你需要一个切片来保持 5 个连接,你可以使用内置的 make()
来实现它:make([]*net.UDPConn, 5)
.
如果你真的需要动态地做到这一点,在运行的时候,是的,反射可以做到这一点。这就是它的样子:
funcType := reflect.TypeOf(net.ListenUDP)
resultType := funcType.Out(0)
arrType := reflect.ArrayOf(4, resultType)
arrValue := reflect.New(arrType).Elem()
conn := &net.UDPConn{}
arrValue.Index(0).Set(reflect.ValueOf(conn))
fmt.Println(arrValue)
这将输出(在 Go Playground 上尝试):
[0xc00000e058 <nil> <nil> <nil>]
查看相关:
C++ 有 decltype(expr)
。您可以声明某个其他表达式类型的对象。例如:
decltype('c') a[4]
将声明一个包含 4 个字符的数组。这是一个玩具示例,但此功能可能很有用。这是 UDP 服务器的一些 Go 代码:
conn, err := net.ListenUDP("udp", udp_addr)
...
defer conn.Close()
...
_, err = conn.WriteToUDP(data, addr)
重要的是我知道我可以做 与(的类型)结果函数(在这种情况下,有连接,ListenUDP
), 但是不知道这个是什么.在这里,因为 Go 的类型推断,我不需要知道。但是如果我想创建 5 个连接,那么我想要一个包含 5 个“ListenUDP
的结果”的数组。我做不到。我得到的最接近的是:
ret_type := reflect.TypeOf(net.DialUDP)
first_param_type := reflect.TypeOf(ret_type.Out(0))
my_arr := reflect.ArrayOf(4, first_param_type)
my_arr[0] = nil
但是最后一行不起作用。有没有办法在 Go 中做到这一点?
Go 没有 compile-time 等同于 C++ 的 decltype
。
但 Go 是一种静态类型语言:即使在短变量声明的情况下存在类型推断,类型在编译时也是已知的。 net.ListenUDP()
are not visible in the source code, but you can look it up just as easily, e.g. it takes 2 seconds to hover over it with your mouse and your IDE will display the signature. Or check online 的结果类型。或者在终端中 运行 go doc net.ListenUDP
。
net.ListenUDP()
的签名是:
func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error)
所以保存 5 个返回连接的数组类型是 [5]*net.UDPConn
。另请注意,在 Go 中使用 slices instead of arrays 更好更容易。
所以我建议使用切片类型 []*net.UDPConn
。如果你需要一个切片来保持 5 个连接,你可以使用内置的 make()
来实现它:make([]*net.UDPConn, 5)
.
如果你真的需要动态地做到这一点,在运行的时候,是的,反射可以做到这一点。这就是它的样子:
funcType := reflect.TypeOf(net.ListenUDP)
resultType := funcType.Out(0)
arrType := reflect.ArrayOf(4, resultType)
arrValue := reflect.New(arrType).Elem()
conn := &net.UDPConn{}
arrValue.Index(0).Set(reflect.ValueOf(conn))
fmt.Println(arrValue)
这将输出(在 Go Playground 上尝试):
[0xc00000e058 <nil> <nil> <nil>]
查看相关: