Initialize an array in Go dynamically

Viewed 40

Can anyone help me on how to initialize an array in Go dynamically? Given is a list of items of a directory:

    entries, err := d.ReadDir(-1)
    count := int64(len(entries))
    array = [count]string{} // invalid array length count

Assuming the goal is to write a function called:

func getFileNamesOfDirectory(path string) []string

Any help is highly appreciated.

1 Answers

No, you can't. Array will be evaluated when the program compiling.

You can code like:

entries, err := d.ReadDir(-1)
count := len(entries)
array = make([]string, count)
Related