[go-nuts] map[X]Y or map[X]*Y ?

2017-05-12 Thread aktungmak
Setting aside the receiver types of methods for the moment, if we consider 
the following declaration:

type Y struct {
//some fields
}

Which of the following would be "better" (from a speed and storage usage 
viewpoint)?

map[X]Y
map[X]*Y

If we consider that this map could be handed between different functions. 
Does it make any difference to use a pointer? Or does it make no 
difference, since the map is already a reference type and functions are 
receiving a copy of the pointer to the map and not a copy of the map value?

Thanks!

-- 
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.


[go-nuts] How to accept any function with one return value as a parameter

2017-03-19 Thread aktungmak
Hi,

I am trying to write a function that initializes a generic collection. For 
adding new items to the collection, the user specifies a constructor which 
takes one argument and returns a pointer to the struct that will be 
inserted in the collection, and also sets fields to initial values. For 
example:

func Constructor(id int) *NewItemX ...
func Constructor(id int) *NewItemY ...
func Constructor(id int) *NewItemZ ...

In the constructor for the collection, I want to accept any function which 
has this general form, although the return type will be different for each 
struct. At first, I thought this might work:

func NewCollection(itemCtr func(int) interface{}) *Collection ...

but of course, trying to pass Constructor fails during compilation since 
the types *NewItemX and interface{} do not match:

.\Collection_test.go:xx: cannot use NewItemX (type func(int) *NewItemX) as 
type func(int) interface {} in argument to NewCollection

I could just do this:

func NewCollection(itemCtr interface{}) *Collection

but then I would have to do some runtime checks using reflect to make sure 
that it is a func etc and I lose compile-time checking of types.

How can I express this best in go?

-- 
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.