duanjucong3124 2016-08-22 07:37 采纳率: 100%
浏览 43
已采纳

通过变量访问函数

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条回答 默认 最新

  • dongtaogou6226 2016-08-22 07:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染