Alex

Reflection (Video 33)

interface{}

var w io.Writer = os.Stdout
f := w.(*os.File) // success
c := w.(*bytes.Buffer) // failure since the interface holds a *os.File not a *bytes.Buffer
c, ok := w.(*bytes.Buffer)
func Println(args ...interface{}) {
    // ...
    for arg := range args {
        switch a := arg.(type) {
            case string: // concrete type
                // do something with the string
            case Stringer: // interface
                // call the String() method on the interface to get the string representation
        }
    }
}

DeepEqual

want := struct{
    someSlice := []int{1,2,3}
}

got := someFunction()

if !reflect.DeepEqual(got, want) {
    fmt.Println("failed equality check")
}