[go-nuts] How to initialize two dimension array

3,959 views
Skip to first unread message

Yang Yang

unread,
May 4, 2010, 12:30:56 PM5/4/10
to golang-nuts
Hi, All

I'm new to Go programming language. I want to initialize a two
dimension array.
But I cannot find how to do it in the document.
Is there a way to initialize [3][3]int in declaration.

Best regards

Yang

Daniel Smith

unread,
May 4, 2010, 12:33:40 PM5/4/10
to Yang Yang, golang-nuts
This should work:

ia := [3][3]int{[3]int{1,2,3},[3]int{1,2,3},[3]int{1,2,3}}
--
Daniel Smith
http://www.schaumburggoclub.org/

Yang Yang

unread,
May 4, 2010, 12:40:50 PM5/4/10
to Daniel Smith, golan...@googlegroups.com
Thanks. However, it looks not very elegant.
IMHO, If Go provides matlab-like array initialization, it will be better.
For example:
ia := [3][3]int{{1,2,3},{1,2,3},{1,2,3}}

Best regards

Yang

2010/5/4 Daniel Smith <luken...@gmail.com>

peterGo

unread,
May 4, 2010, 1:01:58 PM5/4/10
to golang-nuts
Yang,

Has this already been discussed?

a slice of slices
http://groups.google.com/group/golang-nuts/browse_thread/thread/0b1b4bb1085988fd/598f3ae6051c2a7b

Peter

On May 4, 12:40 pm, Yang Yang <comety...@gmail.com> wrote:
> Thanks. However, it looks not very elegant.
> IMHO, If Go provides matlab-like array initialization, it will be better.
> For example:
> ia := [3][3]int{{1,2,3},{1,2,3},{1,2,3}}
>
> Best regards
>
> Yang
>
> 2010/5/4 Daniel Smith <lukenin...@gmail.com>
>
> > This should work:
>
> > ia := [3][3]int{[3]int{1,2,3},[3]int{1,2,3},[3]int{1,2,3}}
>

Kyle Consalus

unread,
May 4, 2010, 1:16:46 PM5/4/10
to Yang Yang, Daniel Smith, golan...@googlegroups.com
On Tue, May 4, 2010 at 9:40 AM, Yang Yang <come...@gmail.com> wrote:
Thanks. However, it looks not very elegant.
IMHO, If Go provides matlab-like array initialization, it will be better.
For example:
ia := [3][3]int{{1,2,3},{1,2,3},{1,2,3}}

hotei

unread,
May 4, 2010, 1:31:00 PM5/4/10
to golang-nuts
Yang,

In my (admittedly short) experience go often provides a way that works
and ... nothing else. If Daniel says that's the way it's done then
it's unlikely there's another way. This is a feature not a bug. It's
one of the reasons you can't take a breath, much less drink a cup of
tea while your program compiles. Personally I'll take this trade-off
and pass up elegance. In most cases (especially mine) elegance had
come to mean "comfortable", ie. like the language I used before. In a
month or so I expect to find go "comfortable" and the problem kind of
just goes away... (no pun intended) (really!) :-)

Hotei

On May 4, 12:40 pm, Yang Yang <comety...@gmail.com> wrote:
> Thanks. However, it looks not very elegant.
> IMHO, If Go provides matlab-like array initialization, it will be better.
> For example:
> ia := [3][3]int{{1,2,3},{1,2,3},{1,2,3}}
>
> Best regards
>
> Yang
>
> 2010/5/4 Daniel Smith <lukenin...@gmail.com>
>
>
>
>
>
> > This should work:
>
> > ia := [3][3]int{[3]int{1,2,3},[3]int{1,2,3},[3]int{1,2,3}}
>

Steven

unread,
May 4, 2010, 2:14:03 PM5/4/10
to hotei, golang-nuts
On Tue, May 4, 2010 at 1:31 PM, hotei <ravens...@cox.net> wrote:
Yang,

In my (admittedly short) experience go often provides a way that works
and ...  nothing else.  If Daniel says that's the way it's done then
it's unlikely there's another way.  This is a feature not a bug.  It's
one of the reasons you can't take a breath, much less drink a cup of
tea while your program compiles.  Personally I'll take this trade-off
and pass up elegance.   In most cases (especially mine) elegance had
come to mean "comfortable", ie. like the language I used before.  In a
month or so I expect to find go "comfortable" and the problem kind of
just goes away... (no pun intended)  (really!)  :-)

Hotei



I highly recommend reading the language spec, effective go, and the various FAQ. They answer a lot of these types of questions. 

Stanley Steel

unread,
May 5, 2010, 9:54:05 AM5/5/10
to Steven, golang-nuts
From the aforementioned link:

"Some of Go's designers had worked on other languages that derived types automatically in such expressions, but the special cases that arise can be messy, especially when interfaces, nil, constant conversions, and such are involved. It seemed better to require the full type information. That way there will be no surprises."


What is an example of a special case?  I don't understand what the problem could be and why it needs to be so verbose.

Russ Cox

unread,
May 5, 2010, 12:41:39 PM5/5/10
to Stanley Steel, Steven, golang-nuts
> What is an example of a special case?

Being able to shorten

var x = [][]int{[]int{1,2,3}}

to

var x = [][]int{{1,2,3}}

but not being able to shorten

var x = []interface{}{[]int{1,2,3}}

to

var x = []interface{}{{1,2,3}}

Russ

Norman Yarvin

unread,
May 8, 2010, 4:25:34 PM5/8/10
to Russ Cox, Stanley Steel, Steven, golang-nuts
I can see not wanting to special-case 'int', but what about
special-casing 'interface'? For static types, once the type is defined
by the outer array declaration, it is easily inferred for the inner ones,
which seems to leave interfaces as the only exceptional case. The
exception seems like it'd be a natural one: when initializing a dynamic
type, one naturally needs to say what static type it contains.


--
Norman Yarvin http://yarchive.net

alexand...@gmail.com

unread,
Aug 18, 2016, 11:33:24 AM8/18/16
to golang-nuts, come...@gmail.com
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for i := 0; i < dy; i++ {
pic[i] = make([]uint8, dx)
for j := 0; j < dx; j++ {
pic[i][j] = uint8(float64(i)*math.Log(float64(j)))
}
}
return pic
}



在 2010年5月5日星期三 UTC+8上午12:30:56,Yang Yang写道:

Seb Binet

unread,
Aug 18, 2016, 12:20:21 PM8/18/16
to alexand...@gmail.com, golang-nuts, come...@gmail.com
On Thu, Aug 18, 2016 at 9:32 AM, <alexand...@gmail.com> wrote:
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for i := 0; i < dy; i++ {
pic[i] = make([]uint8, dx)
for j := 0; j < dx; j++ {
pic[i][j] = uint8(float64(i)*math.Log(float64(j)))
}
}
return pic
}

[][]uint8 is not an array: it's a slice of slices.

OP asked for a 3x3 array.
one can create such a beast like that:
 
hth,
-s


在 2010年5月5日星期三 UTC+8上午12:30:56,Yang Yang写道:
Hi, All

I'm new to Go programming language. I want to initialize a two
dimension array.
But I cannot find how to do it in the document.
Is there a way to initialize [3][3]int in declaration.

Best regards

Yang

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

asit dhal

unread,
Aug 19, 2016, 10:04:21 AM8/19/16
to Seb Binet, alexand...@gmail.com, agl via golang-nuts, come...@gmail.com
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages