In-Method declaration of array of struct literals vs global var?

66 views
Skip to first unread message

a...@google.com

unread,
Jun 6, 2019, 1:56:28 PM6/6/19
to golang-nuts
I can declare literal bar inside a function like this:

func foo() {
  bar := []struct{a, b, c string} {
    { "really", "long", "row"},
    { "many", "many", "rows"},
    ...
  }
}

But I really want it anchored (for code generation reason) outside of a function like this:

func foo() {
  ...
}

var bar []struct{a, b, c string} {
  { "really", "long", "row"},
  { "many", "many", "rows"},
  ...
}

The problem is it gets upset on the last character of the 'var bar ...' line.  Is there a better way to write this?
Note for reasons outside of this discussion, I really want to avoid writing it by repeating the discrete field names:

var bar []struct{a, b, c string} {
  {a: "really",b: "long",c: "row"},  // Don't want to do this
  {a: "many", b: "many", c: "rows"}, // Don't want to do this
  ...
}

Any thoughts?  What am I missing?

Ian Lance Taylor

unread,
Jun 6, 2019, 2:05:54 PM6/6/19
to a...@google.com, golang-nuts
An important tip: when asking a question, please tell us precisely
what you did and precisely what happened. You can see what happened,
but when you write "it gets upset" we have to guess. And it's easy
for us to guess wrong.

When I look at

var bar []struct{a, b, c string} {
{ "really", "long", "row"},
{ "many", "many", "rows"},
...
}

what I think is that it should say

var bar = []struct{...

That is, you are missing the "=". But I don't know if that was a typo
in the e-mail message. So if that doesn't help tell us exactly what
you did and exactly what happened.

Ian

Aaron Spangler

unread,
Jun 6, 2019, 2:47:19 PM6/6/19
to Ian Lance Taylor, golang-nuts
Thank you!  You are correct.  That was exactly my problem.  Adding the equals sign (=) solved it.

I guess I need to be more aware that sometimes an equals statement is required on a 'var' line and other times it is not.  (perhaps only when it has a literal)

Thanks again!

Steven Blenkinsop

unread,
Jun 6, 2019, 10:08:25 PM6/6/19
to Aaron Spangler, Ian Lance Taylor, golang-nuts
= is always required when you're assigning a value to the var. the confusion might be that you can also have a declaration like

var bar []struct{a, b, c string}

where you don't assign a value to bar (meaning it will be null).

Really, the syntax is

var variableName Type = value

You can leave off either the "Type" or the "= value" but not both.

var bar = []struct{a, b, c string} {

  { "really", "long", "row"},
  { "many", "many", "rows"},
  ...
}

is just a special case where you've left off the "Type", and "value" happens to be a composite literal expression. It could also be written as

var bar []struct{a, b, c string} = []struct{a, b, c string} {
  { "really", "long", "row"},
  { "many", "many", "rows"},
  ...
}
--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAHP%2Bjq4-wp5EgvmK-POEHmDNa8-T9WUJqzAYJA9jRsuuo6%2Boaw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Aaron Spangler

unread,
Jun 7, 2019, 2:44:47 PM6/7/19
to Steven Blenkinsop, Ian Lance Taylor, golang-nuts
That makes so much more sense now. Thank you for the explanation!
Reply all
Reply to author
Forward
0 new messages