[go-nuts] Pass variable as size of array ?

10,764 views
Skip to first unread message

kuno

unread,
Apr 20, 2010, 6:27:23 AM4/20/10
to golang-nuts
I do some codes something like this:

===============================
var n int
var array [n]int
===============================

But I got a error info said that:
===============================
invalid array bound n

===============================

Does Go allow pass a variable as the size of an array?


--
Subscription settings: http://groups.google.com/group/golang-nuts/subscribe?hl=en

peterGo

unread,
Apr 20, 2010, 6:41:57 AM4/20/10
to golang-nuts
kuno,

"The length is part of the array's type and must be a constant
expression that evaluates to a non-negative integer value." n is
variable, not a constant expression.
http://golang.org/doc/go_spec.html#Array_types

Peter

Mue

unread,
Apr 20, 2010, 6:50:19 AM4/20/10
to golang-nuts
>
> Does Go allow pass a variable as the size of an array?
>

No, it has to be constant at compile time. So take a slice instead.

s := make([]int, n)

mue

Steven

unread,
Apr 20, 2010, 6:23:18 PM4/20/10
to Mue, golang-nuts
I highly recommend reading the language spec and Effective Go. They
explain all this and more, quite clearly. They aren't very long
either, so you should be able to do it fairly easily.

Guillermo Estrada

unread,
Mar 27, 2012, 10:08:43 PM3/27/12
to golan...@googlegroups.com, Mue
I've searching this group for hours for an example on how to initialize a multidimensional array with a variable at runtime. I have a struct.

type MyType struct {

Guillermo Estrada

unread,
Mar 27, 2012, 10:11:28 PM3/27/12
to golan...@googlegroups.com, Mue
Disregard past post, Chrome does what he wants.

I've searching this group for hours for an example on how to initialize a multidimensional array with a variable at runtime. I have a struct.

type MyType struct {
    somearray [][]int
}

func NewMyType(width, height int) *MyType {
    mt := new(MyType)
    mt := // How do I initialize the 2D slice here with makes?
}

Steven Blenkinsop

unread,
Mar 27, 2012, 10:29:33 PM3/27/12
to Guillermo Estrada, golan...@googlegroups.com, Mue
somearray is a misnomer, since it's really a slice.

 func NewMyType(width, height int) *MyType {
    mt := new(MyType)
    mt.someslice = make([][]int, width)
    for i := range mt.someslice {
        mt.someslice[i] = make([]int, height)
    }
    return mt
}

Guillermo Estrada

unread,
Mar 27, 2012, 11:20:35 PM3/27/12
to golan...@googlegroups.com, Guillermo Estrada, Mue
That's the verbose thing I was expecting, Thanx! I couldn't find anything else (even this one) on initializing at runtime 2D slices. I hope it gets addressed some day to be able to use make to initialize this kind of stuff automatically.
Message has been deleted

Guillermo Estrada

unread,
Mar 28, 2012, 1:09:08 AM3/28/12
to golan...@googlegroups.com, Guillermo Estrada, Mue
I kinda agree with that, you can't expect the language to do your work, but you can also expect to do the work of the language. The fine line between those 2 is what makes a language successful or not. Because a successful language is not measured in features or powerful programming paradigms (ahem.. D lang, I came from there too), but in the amount of people using it to create amazing pieces of software, in the end, you cannot disregard the programmer, in the end, a programming language is for them to use, for humans, not machines. 

If you imagine the master piece of programming languages and put all your effort on your vision of it and don't derail from the path to achieve it, you will be in the end the only one using it, because it will never be finished (D lang again IMHO).

On the other hand if you listen to every single voice shouting in your head on how, why, and where your language should be heading, then in the end you (if only) be the one using it. Because you can't agree with everyone and expect them to be happy.

The fine line on what to lean towards when designing a successful product (yes a programming language is that, and programmers are customers) is constantly moving, and it requires wisdom to know when to listen to the crowd and when to focus on your vision of it. And I love the way Go is doing in that regard. But then again, as the line is moving, we cannot foresee if that issue will be addressed or not, I just hope that if the line moves towards it, Go dev team will have the wisdom they have all shown in this 2 years to address it.

Nice post by the way! I really like it!


On Tuesday, March 27, 2012 9:30:49 PM UTC-6, Peter Thrun wrote:
That's the verbose thing I was expecting, Thanx! I couldn't find anything else (even this one) on initializing at runtime 2D slices. I hope it gets addressed some day to be able to use make to initialize this kind of stuff automatically.

I don't think it will be addressed.  See the fourth paragraph on this page: http://commandcenter.blogspot.com/2011/12/esmereldas-imagination.html

Jan Mercl

unread,
Mar 28, 2012, 4:00:39 AM3/28/12
to golan...@googlegroups.com, Guillermo Estrada, Mue
On Wednesday, March 28, 2012 5:20:35 AM UTC+2, Guillermo Estrada wrote:
That's the verbose thing I was expecting, Thanx! 

If it feels too verbose to you, make it short:

type t [][]int

func makeT(w,h int)(r t){for r=make(t,w);w>0;w--{r[w-1]=make([]int,h)};return}
 
I couldn't find anything else (even this one) on initializing at runtime 2D slices. I hope it gets addressed some day to be able to use make to initialize this kind of stuff automatically.

I hope it doesn't.

André Moraes

unread,
Mar 28, 2012, 7:59:20 AM3/28/12
to golan...@googlegroups.com, Mue
Is this what you want?

package main

import "fmt"

func main() {
data := [][]int{ []int{ 1, 2}, []int {2, 3}}
fmt.Printf("%v",data)
}

And just to make things a little clear, in the case above you don't
have a two dimensional slice, but in fact a slice of slices.

Think in terms of:

type IntSlice []int
type Slice2D []IntSlice

data := make(Slice2D,2)

The language is doing what you are asking it to do, in this case,
allocate a slice with 2 items which type is IntSlice,
Then, later you say to the language to allocate for each position of
the first slice a new slice of ints.

--
André Moraes
http://andredevchannel.blogspot.com/

Jan Mercl

unread,
Mar 28, 2012, 8:14:08 AM3/28/12
to golan...@googlegroups.com
On Wednesday, March 28, 2012 1:59:20 PM UTC+2, André Moraes wrote:
Is this what you want?

package main

import "fmt"

func main() {
        data := [][]int{ []int{ 1, 2}, []int {2, 3}}

           data := [][]int{{1, 2}, {2, 3}} // alt. (http://play.golang.org/p/9FJzIW8VlD)
 

        fmt.Printf("%v",data)
}

 
Reply all
Reply to author
Forward
0 new messages