Well, that didn't go very far: https://go.dev/play/p/UtsrRar9J0Q
Fails with the messages 
./prog.go:15:11: invalid operation: cannot index p_Map (variable of type 
MAP constrained by DataMap)
./prog.go:16:26: cannot range over p_Map (variable of type MAP constrained 
by DataMap) (MAP has no core type) 

I think i have to to some more research here - or just copy and paste three 
different typed functions…

Best regards from Charleston (WV),
     Frank/2
Frank Jüdes schrieb am Mittwoch, 15. März 2023 um 05:51:10 UTC-4:

> Hi Kurtis,
> good to know that a map will always be "kind of" passed by reference to 
> functions. I have verified that with a short test-program on the playground 
> -> https://go.dev/play/p/HxdgpM-QozM
> So in the next code-review all those *[maptype] references will be 
> removed. Things are very different in this case in other programming 
> languages…
>
> I understand the misconception and that a map[type]interface{} won't 
> automagically convert the data-type from the parameter into anything else. 
> My intention was to explain the compiler to just accept whatever is fed 
> into the function and just pass the data through to a function that uses 
> interface{} for a single parameter. That's all this function needs to do: 
> Accept a map of "something", get single values of "something" and pass them 
> on to another function that accepts a single parameter of "something".
>
> I think i have found a solution in using a "generic" interface, stumbled 
> upon this web-site: 
> https://www.geeksforgeeks.org/generics-in-golang/?ref=rp
>
> And created a little test-program: https://go.dev/play/p/mOu1pmmfdX8
>
> This should allow me to pass different types of maps - actually i only 
> need three types - to a generic function that passes the values on to 
> another (generic) function.
>
> Thank you very much for your help.
>
> Best regards from Charleston (WV),
>      Frank/2
>
> Kurtis Rader schrieb am Dienstag, 14. März 2023 um 22:57:13 UTC-4:
>
>> On Tue, Mar 14, 2023 at 7:07 PM Frank Jüdes <jue...@gmail.com> wrote:
>>
>>> Thank you very much Ian!
>>> I am using a pointer to the map because i want to pass the map by 
>>> reference, not by value. 
>>> Those maps can become pretty large and are being handed down through a 
>>> couple of function calls and i don't want them to be copied over and over 
>>> again.
>>>
>>
>> Maps are a special-case. You can't pass them "by value" in the sense you 
>> mean because a "map" value is a very tiny structure that contains a pointer 
>> to the actual map. Possibly the oldest public discussion of the fact maps 
>> are reference type is this blog post: https://go.dev/blog/maps. Using a 
>> pointer does, in fact, avoid copying that very small data structure but is 
>> not necessary to keep from copying the entire map.
>>  
>>
>>> I read the document you referenced, but am not able to understand…
>>> So i can use interface{} or any just to generalize simple types? 
>>> Why is the compiler accepting then a p_Values map[string]interface{} as 
>>> a parameter of a function? - What would be a compatible data-type to pass 
>>> as this parameter?
>>>
>>
>> The compiler accepts a type of "map[string]interface{}" because that is 
>> well defined and obviously useful, Functions, such as those in the fmt 
>> package, would not be useful if a function could not deal with the 
>> "interface{}" type directly or indirectly. Your confusion is very common 
>> and arises from the misconception that the Go map implementation will 
>> magically convert map values from a concrete type (e.g., "int") to the 
>> "any" type if that is what is needed in a particular context. One reason it 
>> doesn't do so is the same reason it doesn't auto-magically convert any 
>> other map value type; e.g., "int" to "float".
>>
>> -- 
>> Kurtis Rader
>> Caretaker of the exceptional canines Junior and Hank
>>
>

-- 
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/abd2cfe9-b651-46a3-bacf-5695446a06d5n%40googlegroups.com.

Reply via email to