Golang之反射
一、关于反射
- 反射功能具有强大的功能
- 反射是用程序检查其所拥有的结构,尤其是类型的一种能力
- 这是元编程的一种形式
- 我们可以在运行时通过反射来分析一个结构体
- 检查其类型和变量(类型和取值)和方法
- 动态地修改变量和调用方法
- 这对于没有源代码的包尤其有用
- 这是一个强大的工具,
二、下面是输出商品到特定JSON文件的源代码
package main
import (
"encoding/json"
"fmt"
"os"
)
type Computer struct {
Name string
Price float64
Cpu string
Memory int
Disk int
}
type Tshirt struct {
Name string
Price float64
Color string
Size int
Sex bool
}
type Car struct {
Name string
Price float64
Cap int
Power string
}
func main() {
products := make([]interface{},0)
products = append(products, Computer{"破坏者",9.9,"i7",32,2048})
products = append(products, Tshirt{"差衣库",88,"Red",40,false})
products = append(products, "Tesla",237811,5,"油电混合")
EncodeObjToJsonFile(products,"E:\\code\\src\\jsonFile\\products.json")
}
func EncodeObjToJsonFile(objs []interface{},filename string) bool {
dstFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
defer dstFile.Close()
encoder := json.NewEncoder(dstFile)
err := encoder.Encode(objs)
if err == nil{
fmt.Println("成功")
return true
}else{
fmt.Println("失败")
return false
}
}
运行显示成功。
下面是输出的JSON文件
然后我们可以去www.json.cn/ 来查看格式化JSON。
三、输出商品到同名JSON文件
package main
import (
"encoding/json"
"fmt"
"os"
"reflect"
)
type Computer struct {
Name string
Price float64
Cpu string
Memory int
Disk int
}
type Tshirt struct {
Name string
Price float64
Color string
Size int
Sex bool
}
type Car struct {
Name string
Price float64
Cap int
Power string
}
func main() {
products := make([]interface{},0)
products = append(products, Computer{"破坏者",9.9,"i7",32,2048})
products = append(products, Tshirt{"差衣库",88,"Red",40,false})
products = append(products, Car{"Tesla",237811,5,"油电混合"})
for _,p := range products{
pValue := reflect.ValueOf(p)
name := pValue.FieldByName("Name").Interface()
fmt.Println(name)
EncodeObjToJsonFile(p,"E:\\区块链殿堂之路\\01.0_Go语言基础-v3.0\\code\\src\\jsonFile\\"+ name.(string) +".json")
}
}
func EncodeObjToJsonFile(obj interface{},filename string) bool {
dstFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
defer dstFile.Close()
encoder := json.NewEncoder(dstFile)
err := encoder.Encode(obj)
if err == nil{
fmt.Println("成功")
return true
}else{
fmt.Println("失败")
return false
}
}
可以看到文件夹中出现了三个JSON文件。
四、优化后代码
import (
"encoding/json"
"fmt"
"os"
)
type IProduct interface {
GetName() string
GetPrice() float64
}
type Product struct {
Name string
Price float64
}
func (p *Product)GetName() string {
return p.Name
}
func (p *Product)GetPrice() float64 {
return p.Price
}
type Computer struct {
//Name string
//Price float64
Product
Cpu string
Memory int
Disk int
}
type Tshirt struct {
//Name string
//Price float64
Product
Color string
Size int
Sex bool
}
type Car struct {
//Name string
//Price float64
Product
Cap int
Power string
}
func main() {
products := make([]IProduct,0)
products = append(products, &Computer{Product{"破坏者",9.9},"i7",32,2048})
products = append(products, &Tshirt{Product{"差衣库",88},"Red",40,false})
products = append(products, &Car{Product{"Tesla",237811},5,"油电混合"})
for _,p := range products{
//通过反射获得Name的值
// pValue := reflect.ValueOf(p)
// name := pValue.FieldByName("Name").Interface()
// fmt.Println(name)
// EncodeObjToJsonFile(p,"E:\\区块链殿堂之路\\01.0_Go语言基础-v3.0\\code\\src\\jsonFile\\"+ name.(string) +".json")
EncodeObjToJsonFile(p,"E:\\区块链殿堂之路\\01.0_Go语言基础-v3.0\\code\\src\\jsonFile\\"+ p.GetName() +".json")
}
}
func EncodeObjToJsonFile(obj interface{},filename string) bool {
dstFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
defer dstFile.Close()
encoder := json.NewEncoder(dstFile)
err := encoder.Encode(obj)
if err == nil{
fmt.Println("成功")
return true
}else{
fmt.Println("失败")
return false
}
}
package main
import (
"encoding/json"
"fmt"
"os"
)
type IProduct interface {
GetName() string
GetPrice() float64
}
type Product struct {
Name string
Price float64
}
func (p *Product)GetName() string {
return p.Name
}
func (p *Product)GetPrice() float64 {
return p.Price
}
type Computer struct {
//Name string
//Price float64
Product
Cpu string
Memory int
Disk int
}
type Tshirt struct {
//Name string
//Price float64
Product
Color string
Size int
Sex bool
}
type Car struct {
//Name string
//Price float64
Product
Cap int
Power string
}
func main() {
products := make([]IProduct,0)
products = append(products, &Computer{Product{"破坏者",9.9},"i7",32,2048})
products = append(products, &Tshirt{Product{"差衣库",88},"Red",40,false})
products = append(products, &Car{Product{"Tesla",237811},5,"油电混合"})
for _,p := range products{
//通过反射获得Name的值
// pValue := reflect.ValueOf(p)
// name := pValue.FieldByName("Name").Interface()
// fmt.Println(name)
// EncodeObjToJsonFile(p,"E:\\区块链殿堂之路\\01.0_Go语言基础-v3.0\\code\\src\\jsonFile\\"+ name.(string) +".json")
EncodeObjToJsonFile(p,"E:\\区块链殿堂之路\\01.0_Go语言基础-v3.0\\code\\src\\jsonFile\\"+ p.GetName() +".json")
}
}
func EncodeObjToJsonFile(obj interface{},filename string) bool {
dstFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
defer dstFile.Close()
encoder := json.NewEncoder(dstFile)
err := encoder.Encode(obj)
if err == nil{
fmt.Println("成功")
return true
}else{
fmt.Println("失败")
return false
}
}
gobench的使用【Go学习笔记】 |Go主题月 juejin.cn/post/694346…
go test的使用【Go 学习笔记】|Go主题月 juejin.cn/post/694319…