Loading

反射

image-20220829192226908

package main

import "fmt"

func main() {

   var a string
   //pair<statictype:string, value:"aceld">
   a = "aceld"

   //pair<type:string, value:"aceld">
   var allType interface{}
   allType = a

   str, _ := allType.(string)
   fmt.Println(str)
}
package main

import (
   "fmt"
   "io"
   "os"
)

func main() {
   //tty: pair<type:*os.File, value:"/dev/tty"文件描述符>
   tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)

   if err != nil {
      fmt.Println("open file error", err)
      return
   }

   //r: pair<type:  , value:>
   var r io.Reader
   //r: pair<type:*os.File, value:"/dev/tty"文件描述符>
   r = tty

   //w: pair<type:  , value:>
   var w io.Writer
   //w: pair<type:*os.File, value:"/dev/tty"文件描述符>
   w = r.(io.Writer)

   w.Write([]byte("HELLO THIS is A TEST!!!\n"))
}
package main

import "fmt"

type Reader interface {
   ReadBook()
}

type Writer interface {
   WriteBook()
}

//具体类型
type Book struct {
}

func (this *Book) ReadBook() {
   fmt.Println("Read a Book")
}

func (this *Book) WriteBook() {
   fmt.Println("Write a Book")
}

func main() {
   //b: pair<type:Book, value:book{}地址>
   b := &Book{}

   //r: pair<type:, value:>
   var r Reader
   //r: pair<type:Book, value:book{}地址>
   r = b

   r.ReadBook()

   var w Writer
   //r: pair<type:Book, value:book{}地址>
   w = r.(Writer) //此处的断言为什么会成功? 因为w r 具体的type是一致

   w.WriteBook()
}
package main

import "fmt"

type Reader interface {
   ReadBook()
}

type Writer interface {
   WriteBook()
}

//具体类型
type Book struct {
}

func (this *Book) ReadBook() {
   fmt.Println("Read a Book")
}

func (this *Book) WriteBook() {
   fmt.Println("Write a Book")
}

func main() {
   //b: pair<type:Book, value:book{}地址>
   b := &Book{}

   //r: pair<type:, value:>
   var r Reader
   //r: pair<type:Book, value:book{}地址>
   r = b

   r.ReadBook()

   var w Writer
   //r: pair<type:Book, value:book{}地址>
   w = r.(Writer) //此处的断言为什么会成功? 因为w r 具体的type是一致

   w.WriteBook()
}
package main

import (
   "fmt"
   "reflect"
)

type User struct {
   Id   int
   Name string
   Age  int
}

func (this *User) Call() {
   fmt.Println("user is called ..")
   fmt.Printf("%v\n", this)
}

func main() {
   user := User{1, "Aceld", 18}

   DoFiledAndMethod(user)
}

func DoFiledAndMethod(input interface{}) {
   //获取input的type
   inputType := reflect.TypeOf(input)
   fmt.Println("inputType is :", inputType.Name())
   fmt.Println("inputType is :", inputType)
   //获取input的value
   inputValue := reflect.ValueOf(input)
   fmt.Println("inputValue is:", inputValue)

   //通过type 获取里面的字段
   //1. 获取interface的reflect.Type,通过Type得到NumField ,进行遍历
   //2. 得到每个field,数据类型
   //3. 通过filed有一个Interface()方法等到 对应的value
   for i := 0; i < inputType.NumField(); i++ {
      field := inputType.Field(i)
      value := inputValue.Field(i).Interface()

      fmt.Printf("%s: %v = %v\n", field.Name, field.Type, value)
   }
   fmt.Printf("--------------------------")
   //通过type 获取里面的方法,调用
   for i := 0; i < inputType.NumMethod(); i++ {
      m := inputType.Method(i)
      fmt.Printf("%s: %v\n", m.Name, m.Type)
   }

}
posted @ 2022-08-29 19:59  suehoo  阅读(20)  评论(0编辑  收藏  举报