douzhongpi9734 2013-10-12 05:52
浏览 27
已采纳

Golang,Go:具有返回接口的映射?

http://golang.org/pkg/sort/

This is from Go example.

 // OrderedBy returns a Sorter that sorts using the less functions, in order.
// Call its Sort method to sort the data.
func OrderedBy(less ...lessFunc) *multiSorter {
    return &multiSorter{
        changes: changes,
        less:    less,
    }
}

What does this do by colon? Is it mapping? Is it closure? Too much new syntax here. What should I read to understand this syntax in Go?

  • 写回答

2条回答 默认 最新

  • duancoubeng5909 2013-10-12 06:08
    关注

    It's a factory function, creating and initialising a struct of type multisorter:

    https://sites.google.com/site/gopatterns/object-oriented/constructors

    Additionally, Go "constructors" can be written succinctly using initializers within a factory function:

    function NewMatrix(rows, cols, int) *matrix {
        return &matrix{rows, cols, make([]float, rows*cols)}
    }
    

    Also, it is using named parameters when initialising:

    http://www.golang-book.com/9

    This allocates memory for all the fields, sets each of them to their zero value and returns a pointer. (Circle) More often we want to give each of the fields a value. We can do this in two ways. Like this:

    c := Circle{x: 0, y: 0, r: 5}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 软件定义网络mininet和onos控制器问题
  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。