Re: [go-nuts] How to generalize parameters for a function
On Wed, 2023-03-15 at 12:55 -0400, Frank Juedes wrote: > > > Hi Dan, > Thank you very much for your answer, so that's the data structure > behind maps, very interesting. > I had actually thought about using unsafe pointers and then type- > casting, but that is how i would have done it in the C-dekades, not > with Go. The source for this is here https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/runtime/map.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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/794a286af50a58c9ac1e38fec6f7f10ce5d4f4a9.camel%40kortschak.io.
Re: [go-nuts] How to generalize parameters for a function
On 2023-03-15 06:06, Jan Mercl wrote: On Wed, Mar 15, 2023 at 11:03 AM Frank Jüdes wrote: 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) https://go.dev/play/p/6KN36eeomML Hi Jan, that looks great and is exactly what i was looking for, just need to wrap my head around that function declaration… I understand /that/*//*declaration, but do not fully understand this in general. I guess i will have to do some more reading… Thank you very much! Best regards from Charleston (WV), Frank/2 -- 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/5d13aa53-2ca8-13c4-cf12-38cb61c9e7f3%40gmail.com.
Re: [go-nuts] How to generalize parameters for a function
Which is also the reason why you can't insert into a nil map - you have to make() one. On Wednesday, 15 March 2023 at 10:58:09 UTC Dan Kortschak wrote: > On Tue, 2023-03-14 at 19:56 -0700, Kurtis Rader wrote: > > 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. > > The passed value is a pointer to the header, otherwise changes to maps > would require the altered map to be returned as with slices and append. > > https://go.dev/play/p/avBBkNwVZRY > > -- 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/0ffd9d0d-b80a-4ec5-a772-40dfb102b43an%40googlegroups.com.
Re: [go-nuts] How to generalize parameters for a function
On Tue, 2023-03-14 at 19:56 -0700, Kurtis Rader wrote: > 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. The passed value is a pointer to the header, otherwise changes to maps would require the altered map to be returned as with slices and append. https://go.dev/play/p/avBBkNwVZRY -- 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/4af2d37285e3b6d2a7594342c18f5c4160b7b86f.camel%40kortschak.io.
Re: [go-nuts] How to generalize parameters for a function
On Wed, Mar 15, 2023 at 11:03 AM Frank Jüdes wrote: > > 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) https://go.dev/play/p/6KN36eeomML -- 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/CAA40n-WAqUuSR_896rz4C%3DSDOH3LGxZhC4DQ8%2BEVqh%2BM0JKUKw%40mail.gmail.com.
Re: [go-nuts] How to generalize parameters for a function
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 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.
Re: [go-nuts] How to generalize parameters for a function
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 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/35a85e3c-be23-4e61-8b59-fcdd2f93a4ecn%40googlegroups.com.
Re: [go-nuts] How to generalize parameters for a function
On Tue, Mar 14, 2023 at 7:07 PM Frank Jüdes 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/CABx2%3DD8eKMBwJgQ_Qvn163mZz3%3D4tZ18Lpugp5fO_M1hSj68RA%40mail.gmail.com.
Re: [go-nuts] How to generalize parameters for a function
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. 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? Thank you very much in advance for your help. Best regards from Charleston (WV), Frank/2 Ian Lance Taylor schrieb am Dienstag, 14. März 2023 um 21:40:27 UTC-4: > On Tue, Mar 14, 2023 at 6:27 PM Frank Jüdes wrote: > > > > i still somewhat new with Go and need a little tutoring: I have to > create Excel files, using the excelize package and wrote a little function > to store data from a map[string]int64 into a column within the worksheet: > > > > // > - > > // Store a slice of int64 data items into the cells of a column > > // > - > > func SetColumnValuesInt64(xf *excelize.File, > > p_SheetName string, > > p_StartRow int, > > p_Column rune, > > p_CellStyle *excelize.Style, > > p_Keys *UTL_StringSlice.T_StringSlice, > > p_Values *map[string]int64) { > > Row := p_StartRow > > StyleID,_ := xf.NewStyle(p_CellStyle) > > for _,Key := range(*p_Keys) { > > CellAddress := fmt.Sprintf("%c%d",p_Column,Row) > > xf.SetCellValue(p_SheetName,CellAddress,(*p_Values)[Key]) > > xf.SetCellStyle(p_SheetName,CellAddress,CellAddress,StyleID) > > Row++ > > } // END for > > } // END SetColumnValuesInt64 > > > > Basically the function iterates through a slice of strings, containing > the key-values for the data-map and then calls the SetCellValue function > from the excelize package to store the number into the cell. > > Now i have another set of data, stored into a map[string]float64 … > > So i duplicated the above function, just replacing the red line with > > p_Values *map[string]float64) { > > > > Basically it is the same code, just the parameter-type is different. I > looked into the definition of the function SetCellValue and saw that it is > using the empty interface as parameter for the value, so i tried to define > my function with > > p_Values *map[string]interface{}) { > > > > The function compiles fine, but if i try to use it with a > *map[string]int64 as a parameter, the compiler objectst with the message > »cannot use Amounts (variable of type *map[string]int64) as type > *map[string]interface{} in argument to ExcelFormats.SetColumnValues« > > > > Can somebody please give me hint what i am doing wrong? > > First I'll note that it's quite unusual to use a pointer to a map. > Maps are reference types. If you pass a map to a function, and that > function adds values to the map, the caller will also see the new map > entries. > > That said, the error message seems clear enough: a function that > expects a pointer to map[string]any can't take an argument that is a > pointer to map[string]int64. The types any and int64 are different > types. You can't interchange them. See > https://go.dev/doc/faq#covariant_types for a related issue. > > 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/48a30be4-9229-4c6c-9503-12cbaa90c35an%40googlegroups.com.
Re: [go-nuts] How to generalize parameters for a function
On Tue, Mar 14, 2023 at 6:27 PM Frank Jüdes wrote: > > i still somewhat new with Go and need a little tutoring: I have to create > Excel files, using the excelize package and wrote a little function to store > data from a map[string]int64 into a column within the worksheet: > > // > - > // Store a slice of int64 data items into the cells of a column > // > - > func SetColumnValuesInt64(xf *excelize.File, > p_SheetName string, > p_StartRow int, > p_Column rune, > p_CellStyle *excelize.Style, > p_Keys *UTL_StringSlice.T_StringSlice, > p_Values *map[string]int64) { >Row := p_StartRow >StyleID,_ := xf.NewStyle(p_CellStyle) >for _,Key := range(*p_Keys) { > CellAddress := fmt.Sprintf("%c%d",p_Column,Row) > xf.SetCellValue(p_SheetName,CellAddress,(*p_Values)[Key]) > xf.SetCellStyle(p_SheetName,CellAddress,CellAddress,StyleID) > Row++ >} // END for > } // END SetColumnValuesInt64 > > Basically the function iterates through a slice of strings, containing the > key-values for the data-map and then calls the SetCellValue function from the > excelize package to store the number into the cell. > Now i have another set of data, stored into a map[string]float64 … > So i duplicated the above function, just replacing the red line with > p_Values *map[string]float64) { > > Basically it is the same code, just the parameter-type is different. I looked > into the definition of the function SetCellValue and saw that it is using > the empty interface as parameter for the value, so i tried to define my > function with > p_Values *map[string]interface{}) { > > The function compiles fine, but if i try to use it with a *map[string]int64 > as a parameter, the compiler objectst with the message »cannot use Amounts > (variable of type *map[string]int64) as type *map[string]interface{} in > argument to ExcelFormats.SetColumnValues« > > Can somebody please give me hint what i am doing wrong? First I'll note that it's quite unusual to use a pointer to a map. Maps are reference types. If you pass a map to a function, and that function adds values to the map, the caller will also see the new map entries. That said, the error message seems clear enough: a function that expects a pointer to map[string]any can't take an argument that is a pointer to map[string]int64. The types any and int64 are different types. You can't interchange them. See https://go.dev/doc/faq#covariant_types for a related issue. 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/CAOyqgcVnv5VE%3DF-Hdd7vMPUzQHqUDJNPtD_SDK2TSL3q-hcDcg%40mail.gmail.com.