>> How does the generic function know the size of the return value?

All your judgement are not correct only because you ask not a correct 
questions.

They return pointers to appropriate values.
Or you really think that the `map` also returns a value instead of pointer?
How it (map) can know the size of the return value
The `map` in Go language also generic type.

Caller (parametrized code):

var m map[Foo]Baz
...
v := m[Foo{}]

Callee (generice code):

// mapaccess1 returns a pointer to h[key].  Never returns nil, instead// it 
will return a reference to the zero object for the value type if// the key is 
not in the map.// NOTE: The returned pointer may keep the whole map live, so 
don't// hold onto it for very long.func mapaccess1(t *maptype, h *hmap, key 
unsafe.Pointer) unsafe.Pointer {


Returned value will be obtained in parametrized code (where m is map[Foo]Baz) 
from the pointer because both (caller and  callee which is instance of the 
map[Foo]Baz) operates with the values of the same types.

The `map` does not know the types and its sizes. It only know that it 
instantiated (by the type argements) at the runtime "to be ready to work for 
these types".

This approach is called "generices programming" and it used in Go language in 
the following types:

- Channels

- Slices

- Maps


P.S.

// NOTE: The returned pointer may keep the whole map live, so don't// hold onto 
it for very long.


This note not for the programmers. This not for the developers.

This is because programmers always gets a value from the dereferenced pointers 
but developers can call this function directly.

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