= 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"},
  ...
}

On Thu, Jun 6, 2019 at 2:47 PM 'Aaron Spangler' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> 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!
>
> On Thu, Jun 6, 2019 at 11:05 AM Ian Lance Taylor <i...@golang.org> wrote:
>
>> On Thu, Jun 6, 2019 at 10:56 AM ajs via golang-nuts
>> <golang-nuts@googlegroups.com> wrote:
>> >
>> > 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?
>>
>> 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
>>
> --
> 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+unsubscr...@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
> <https://groups.google.com/d/msgid/golang-nuts/CAHP%2Bjq4-wp5EgvmK-POEHmDNa8-T9WUJqzAYJA9jRsuuo6%2Boaw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CANjmGJsibJ5qVZ%3DTMW6FOU6VJtVKHP4uFjkN6wyf-sU8QOWrTg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to