0
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?

1
  • You read Go code from left to right like normal english (not in spirals like C or kinda backwards like Java). [] "slice of" func function, ( "from argument", * pointer to, someType "someType", ) "returning an", error "error". Could not be much simpler. Commented Sep 19, 2018 at 4:37

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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) },
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.