doushi6932
2018-09-18 23:54 阅读 104
已采纳

go中的函数数组语法是什么意思?

var list = []func(*someType) error {
        ...
    }

I am new to Go and I am trying to understand what does the syntax mean ? Is the return of function an array?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dtamho6404 dtamho6404 2018-09-19 00:03

    This declares and initializes a variable list as a slice whose elements are functions with signature func(*someType) error.

    Slices in Go are convenient mechanisms for representing sequences of data of a particular type. They have type []T for any element type T (but remember Go does not have generics). A slice is defined only by the type of the items it contains; its length is not part of its type definition and can change at runtime. (Arrays in Go, by contrast, are of fixed length - their type is [N]T for length N and element type T).

    Under the surface, a slice consists of a backing array, a length of the current data and a capacity. The runtime manages the memory allocation of the array to accommodate all data in the slice.

    点赞 评论 复制链接分享
  • douqin0676 douqin0676 2018-09-19 00:20

    func in go is a type like int,string...

    So they are sample syntax:

    var listInt := []int{1,2,3}
    var listStr := []string{"1","2","3"}
    var listFunc := []func(param anyType) anyType {
        func(param anyType) anyType { ... return new(anyType) },
        func(param anyType) anyType { ... return new(anyType) },
    }
    
    点赞 评论 复制链接分享

相关推荐