Go函数通过变量访问 [英] Go functions accessed through variables

查看:105
本文介绍了Go函数通过变量访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



功能如下:

<$ p
append(slice,0)//用于切片
len(切片),cap(切片)//再次为切片

等等。作为一个来自C语言的人,我想知道:

1)这些函数是否可以通过变量本身调用(如 map.delete (答案))?



2)这是一种常见的做法(定义一个通用函数并让它计算出类型和内容它应该这样做),还是仅仅为内置类型。例如,如果我要定义自己的类型,比如 MyCoolLinkedList ,我应该定义 len 并在类型,并让它们像

那样调用

$ p $ list:= new(MyCoolLinkedList)
list.len()

或者我应该定义一个接收列表的函数,如:

  len(list)


解决方案

div>

1 - 您不能将内置方法附加为类型或值,例如

  m:= map [int] string {1:one} 
m.delete(1)

是一个编译时错误,您可以轻松验证



2 - Go没有泛型。但为了缓解痛苦,它提供了几个内置功能,它可以接受不同类型的值。它们是内置的,因为 - 如前所述 - 由于缺乏泛型,它们需要编译器的帮助才能接受不同类型的值。有些还接受类型而不是表达式作为第一个参数(例如 make([] int,1)),这也是您无法创建的。内建函数没有标准的Go类型,它们只能出现在调用表达式中。



您无法创建接受不同类型值的函数。话虽如此,当您创建自己的类型并为其创建一个函数时,建议将其声明为方法而不是辅助函数;就好像函数在你的具体类型上运行一样,你无法将它用于其他类型。

所以将它声明为方法是有意义的,然后你可以称它更优雅,如

  value.Method()

作为一种方法也会计数到方法集的类型,你是否需要实现一个接口,例如如果你的 MyCoolLinkedList 那么实现 sort.Interface 可以对列表进行排序,这需要 Len()int 方法例如。

通过辅助函数选择方法还有一个好处,即您的方法可以通过反射来使用。您可以使用 reflect 包,但不能用just函数来完成。


I am just starting to study Go and some things caught my attention.

Functions like:

delete(map, "Answer") // for maps
append(slice, 0) // for slices
len(slice), cap(slice) // again for slices

and so on. As someone coming from C like languages, I am wondering:

1) Can these functions be called through the variable itself (as in map.delete("Answer"))?

2) Is this a common practice (defining a generic function and let it figure out the type and what it should do), or is this just for the built in types. For example, if I would define my own type, like MyCoolLinkedList, should I define the len and append functions inside the type and have them called like

list := new(MyCoolLinkedList)
list.len()

or should I define a function that receives the list, like:

len(list)

解决方案

1 - You can't call builtin methods "attached" to types or values, e.g.

m := map[int]string{1: "one"}
m.delete(1)

is a compile time error which you can verify easily.

2 - Go doesn't have generics. But to ease the "pain", it provides several builtin functions which can accept values of different types. They are builtin because –as mentioned– due to the lack of generics, they need the help of the compiler to be able to accept values of different types. Some also accept a type instead of an expression as the first argument (e.g. make([]int, 1)) which is also something you can't create. Builtin functions do not have standard Go types, they can only appear in call expressions.

You can't create such functions that accept values of different types. Having said that, when you create your own type and you create a "function" for it, it is advisable to declare it as a method instead of a helper function; as if the "function" operates on your concrete type, you could not use it for other types anyway.

So it makes sense to declare it as a method, and then you can call it more "elegantly" like

value.Method()

Being a method also "counts" toward the method set of the type, should you need to implement an interface, e.g. in case of your MyCoolLinkedList it would make sense to implement sort.Interface to be able to sort the list, which requires a Len() int method for example.

Choosing a method over a helper function also has the advantage that your method will be available via reflection. You can list and call methods of a type using the reflect package, but you can't do the same with "just" functions.

这篇关于Go函数通过变量访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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