Hi Frank, the issue is that the compiler won't accept a map[string]float64 
as a map[string]interface{} because they are defined as different types.
It probably feels to you as though the code would work okay because you 
expect to take things out of the map, check their type and then use them 
appropriately.
However, the compiler doesn't attempt to check that's really what you're 
doing..

Instead, the compiler has to assume your code might try to use the map to 
place things into it.
If you ask for a map[string]interface{}, are given a map[string]float64, 
and then try to put tulips into it, the code will fail.

This is an example of type variance, for which Ian has linked a much nicer 
and more concise answer than I could hope to type out.
On Tuesday, March 14, 2023 at 7:27:05 PM UTC-6 Frank Jüdes wrote:

> Hi Friends,
>
> 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?
>
> Thank you very much in advance for your help.
>
> 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/715e8be0-0e36-41e5-8966-3c64c6233031n%40googlegroups.com.

Reply via email to