The spec defines: "A nil map is equivalent to an empty map except that no
elements may be added." https://golang.org/ref/spec#Map_types

You could return a nil map, if you have no elements to add, and a caller
would just need to read.

On Tue, Oct 17, 2017 at 7:02 AM Dan Kortschak <dan.kortsc...@adelaide.edu.au>
wrote:

> If you only need the map conditionally, you need to declare it and then
> conditionally make it.
>
> ```
> var m map[string]int
> if needMap {
>     m = make(map[string]int)
> }
> ```
>
> On Mon, 2017-10-16 at 21:52 -0700, Alex Dvoretskiy wrote:
> > Hello, Golang Nuts!
> >
> > I have an interesting question about maps. What is the possible usage
> > of
> > nil maps, which can be declared like "var m map[string]int"? You
> > can't
> > write to nil map but have an option to create it.
> >
> > Perhaps there is no use at all and this is just language specific
> > feature?
> >
> > Thank you.
>
>
>
> --
> 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.
> 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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to