1

I would have expected this code to work:

package main

type Item struct {
  Key string
  Value string
}

type Blah struct {
  Values []Item
}

func main() {
  var list = [...]Item {
    Item {
      Key : "Hello1",
      Value : "World1",
    },
    Item {
      Key : "Hello1",
      Value : "World1",
    },
  }

  _ = Blah {
    Values : &list,
  }
}

I thought this would be the correct way of doing this; Values is a slice, list is an array. &list should be a slice, which is assignable to Item[], right?

...but instead, it errors with the message:

cannot use &list (type *[2]Item) as type []Item in assignment

In C, you'd write:

struct Item {
  char *key;
  char *value;
};

struct Blah {
   struct Item *values;
};

How do you do that in Go?

I saw this question: Using a pointer to array

...but either the answers are for a previous version of Go, or they're just plain wrong. :/

Community
  • 1
  • 1
Doug
  • 32,844
  • 38
  • 166
  • 222

3 Answers3

4

A slice is not simply a pointer to an array, it has an internal representation which contains its length and capacity.

If you want to get a slice from list you can do:

_ = Blah {
    Values : list[:],
}
lbonn
  • 2,499
  • 22
  • 32
3

Go is, fortunately, not so verbose as it might seem from the OP. This works:

package main

type Item struct {
        Key, Value string
}

type Blah struct {
        Values []Item
}

func main() {
        list := []Item{
                {"Hello1", "World1"},
                {"Hello2", "World2"},
        }

        _ = Blah{list[:]}
}

(Also here)

PS: Let me suggest to not write C in Go.

zzzz
  • 87,403
  • 16
  • 175
  • 139
  • Is there a point to your comment about C code? It seems irrelevant to the question. The example could have been from any language; it was simply a 'here is how you would do it in X, what is the way of doing this in go?' – Doug Feb 11 '13 at 07:53
  • 2
    Yes, there is a point to it. For example the OP shows how thinking in C while coding in Go easily leads to misconceptions about Go arrays and Go slices. `v []T` in Go is not the same as `T []v` in C. That's not the only place where to fall in such traps. Go resembles C a lot, while actually not being that much close to C in many aspects. PS: The more than necessary verbose composite literal is a C-ism as well, IMO. – zzzz Feb 11 '13 at 08:15
  • Seems unnecessarily like a 'You're doing it wrong!!' response, to a simple question. :( – Doug Feb 11 '13 at 08:43
  • 1
    Intended as a hint how to do things better. – zzzz Feb 11 '13 at 10:02
  • 2
    @Doug perhaps you might have edited the answer yourself to remove the PS. It would have been more constructive. Aside from this debatable point, it is a good answer. – Rick-777 Feb 11 '13 at 19:40
2

When you are starting out with Go ignore arrays completely and just use slices is my advice. Arrays are rarely used and cause Go beginners a lot of trouble. If you have a slice then you don't need a pointer to it since it is a reference type.

Here is your example with a slice and no pointers which is much more idiomatic.

package main

type Item struct {
    Key   string
    Value string
}

type Blah struct {
    Values []Item
}

func main() {
    var list = []Item{
        Item{
            Key:   "Hello1",
            Value: "World1",
        },
        Item{
            Key:   "Hello1",
            Value: "World1",
        },
    }

    _ = Blah{
        Values: list,
    }
}
Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132