Answer the question
In order to leave comments, you need to log in
How to dynamically allocate array [var]type?
Obviously it doesn't compile.
length := 7
var array [length]int
const length = 7
var array [length]int
//or
length := 7
slice := make([]int, length) //we get a slice here instead of array
func UseArray(length int){
var array //of length ???????
for _, value := range array{ //do something with array
fmt.Println(value)
}
}
Answer the question
In order to leave comments, you need to log in
In any way - for escape analysis your array all the same can put in hip.
```
package main
import (
"fmt"
)
func z(i int) {
var s [10]int
for n := 0; n<i; n++ {
s[n] = n
}
fmt.Printf("%v", s)
}
func main() {
z(10)
}
```
```
$ go build -gcflags="-m" main.go
# command-line-arguments
./main.go:12:12: z ... argument does not escape
```
Slices use the same arrays under the hood.
Using:
length := 7
slice := make([]int, length)
to control allocation for example
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question