Empty_Interface

  • Empty interface specifies no methods.
  • We can assign any value to the empty interface.
  • Empty interface gives no knowledge of about data coming in.
  • Benefits of static typed language is nullified.
  • Neet to use reflect library to turn arbitrary(隨意的) structs into specific type.
  • Prefer to use specific data type.
  • Create an interface with some specific methods that we need.

Exercises

https://go.dev/play/p/DLKPlO4JOfe

(42, int) (hello, string)

package main

import "fmt"

func main() {
    describe(42)
    describe("hello")
}

func describe(i interface{}) {
    fmt.Printf("(%v, %T)\n", i, i)
}
© Kimi Tsai all right reserved.            Updated : 2023-07-12 09:04:53

results matching ""

    No results matching ""

    results matching ""

      No results matching ""