Interface iteration

I want to create a function that takes either a map or an array of any iteration over it, calling a function for each element that knows what to do with any types it encounters.

Here is my first unsuccessful attempt. Currently, when I run it in my real use case, it always says “uh oh!”.

func DoTheThingToAllTheThings(data_interface interface{}) int {
    var numThings int

    switch data := data_interface.(type) {
    case map[interface{}]interface{}:
        numThings = len(data)
        for index, item := range data {
            DoTheThing(index, item)
    }
    case []interface{}:
        numThings = len(data)
        for index, item := range data {
            DoTheThing(index, item)
        }
    default:
        fmt.Println("uh oh!")
    }

    return numThings
}

An array or map can contain many different things, so there is no way to try and compare all possible input data.

Otherwise, is there a way to iterate over an array or map in Go, not knowing exactly what it is?

+1
source share
2 answers

fmt.Printf("%v\n", data_interface) , . , .

: http://golang.org/src/pkg/fmt/print.go?h=printArg#L708

printArg - :

return p.printReflectValue(reflect.ValueOf(arg), verb, plus, goSyntax, depth

"reflection": http://golang.org/pkg/reflect/ . p.printReflectValue : http://golang.org/src/pkg/fmt/print.go?h=printArg#L862 , . printValue .

, , . , , .

package main

import (
    "fmt"
    "reflect"
)

type Player string

type Board struct {
    Tboard  [9]string
    Player1 Player
    Player2 Player
}

// ignore this function contents, I grabbed it from elsewhere.
func makeBoard() *Board {
    b := &Board{Tboard: [9]string{}}
    for x := 0; x < len(b.Tboard); x++ {
        b.Tboard[x] = "X"
        fmt.Println(b.Tboard[x])
    }
    b.Player1 = "George"
    b.Player2 = "Tim"

    fmt.Printf("Len: %v\n", len(b.Tboard)) // => 9

    fmt.Printf("Contents: %v\n", b)
    fmt.Printf("Syntax: %#v\n", b)
    fmt.Printf("Type: %T\n", b)
    fmt.Println("Board:", b.Tboard)
    return b
}

func main() {
    myBoard := makeBoard()

    v := reflect.ValueOf(*myBoard) // v is of type Value
    t := v.Type()

    fmt.Printf("Value: %v %T\n", v, v)
    fmt.Printf("Type:  %v %T\n", t, t)

    // would be a switch
    if t == reflect.TypeOf(*myBoard) {
        var b2 Board

        b2 = v.Interface().(Board) // cast the type interface{} into type Board
        fmt.Printf("v converted back to: %#v\n", b2)
    } else {
        fmt.Printf("t is not recognized")
    }

}

, v - main.Board, , Board. , , .

+1

, interface{} ( ), :

// This won't work, as the .(type) would be []S
someS := []S{S{}}
DoTheThingToAllTheThings(someS)

// This will: it will go in case []interface{}:
someSI := make([]interface{}, len(someS))
for i, s := range someS {
    someSI[i] = s
}
DoTheThingToAllTheThings(someSI)

.

, interface{} DoTheThing.
, " Go?".

+1

Source: https://habr.com/ru/post/1661334/


All Articles