目录结构:
I:\workspace_goland\hello>tree /F
卷 新加卷 的文件夹 PATH 列表
卷序列号为 F04C-09C0
I:.
├─bin
├─pkg
└─src
│ main.go
│ system.json
│
└─com
├─bo
│ adsl.go
│ lan.go
│
└─conf
system.go
main.go
@H_403_29@package main @H_403_29@import ( @H_403_29@"com/bo" @H_403_29@ "com/conf" @H_403_29@ "fmt" @H_403_29@ "reflect" @H_403_29@ "strconv" @H_403_29@ "strings" ) @H_403_29@var regStruct @H_403_29@map[string]@H_403_29@interface{} @H_403_29@var sys conf.System @H_403_29@func init() { conf.LoadConfig(@H_403_29@"I:/workspace_goland/hello/src/system.json",&sys) regStruct = make(@H_403_29@map[string]@H_403_29@interface{}) regStruct[@H_403_29@"Adsl"] = bo.Adsl{} regStruct[@H_403_29@"Lan"] = bo.Lan{} } //golang按请求中的不同标识处理不同的业务逻辑 @H_403_29@func main() { request := @H_403_29@"3|||1|||P01=1|||M02=3" @H_403_29@ doRequest(request,sys) request = @H_403_29@"4|||2|||P01=1|||M02=3" @H_403_29@ doRequest(request,sys) } @H_403_29@func doRequest(request string,sys conf.System) { //解析请求,获取业务对象,操作对象和其他数据 fmt.Println(@H_403_29@"request: ",request) fields := strings.Split(request,@H_403_29@"|||") objtype,_ := strconv.Atoi(fields[0]) opttype,_ := strconv.Atoi(fields[1]) ruleClassName := getRuleClassName(sys,objtype) methodName := getOptMethodName(sys,opttype) execute(ruleClassName,methodName) } //通过反射调用对象的操作方法 @H_403_29@func execute(ruleClassName string,methodName string) { t := reflect.TypeOf(regStruct[ruleClassName]) response := reflect.New(t).MethodByName(methodName).Call(nil) fmt.Println(@H_403_29@"response: ",response) fmt.Println() } @H_403_29@func getOptMethodName(sys conf.System,opttype int) string { @H_403_29@var ret = @H_403_29@"" @H_403_29@ optList := sys.OptList @H_403_29@for _,obj := @H_403_29@range optList { @H_403_29@if opttype == obj.OptType { ret = obj.MethodName fmt.Println(@H_403_29@"methodName: ",ret) @H_403_29@break @H_403_29@ } } @H_403_29@return ret } @H_403_29@func getRuleClassName(sys conf.System,objtype int) string { @H_403_29@var ret = @H_403_29@"" @H_403_29@ objList := sys.ObjList @H_403_29@for _,obj := @H_403_29@range objList { @H_403_29@if objtype == obj.ObjType { ret = obj.RuleClassName fmt.Println(@H_403_29@"RuleClassName: ",ret) @H_403_29@break @H_403_29@ } } @H_403_29@return ret }
adsl.go
@H_403_29@package bo @H_403_29@type Adsl @H_403_29@struct { } @H_403_29@func (obj Adsl) Add() string { ret := @H_403_29@"invoke adsl add,then return data" @H_403_29@ @H_403_29@return ret } @H_403_29@func (obj Adsl) Del() string { ret := @H_403_29@"invoke adsl delete,then return data" @H_403_29@ @H_403_29@return ret }
lan.go
@H_403_29@package bo @H_403_29@type Lan @H_403_29@struct { } @H_403_29@func (obj Lan) Add() string { ret := @H_403_29@"invoke lan add,then return data" @H_403_29@ @H_403_29@return ret } @H_403_29@func (obj Lan) Del() string { ret := @H_403_29@"invoke lan delete,then return data" @H_403_29@ @H_403_29@return ret }
system.go
@H_403_29@package conf @H_403_29@import ( @H_403_29@"encoding/json" @H_403_29@ "fmt" @H_403_29@ "io/IoUtil" ) @H_403_29@type System @H_403_29@struct { ObjList []obj @H_403_29@`json:"objList"` @H_403_29@ OptList []opt @H_403_29@`json:"optList"` } @H_403_29@type obj @H_403_29@struct { ObjType int @H_403_29@`json:"objType"` //变量首字母必须大写,不然解析不出来数据 RuleClassName string @H_403_29@`json:"ruleClassName"` @H_403_29@ ServiceType int @H_403_29@`json:"serviceType"` } @H_403_29@type opt @H_403_29@struct { OptType int @H_403_29@`json:"optType"` @H_403_29@ MethodName string @H_403_29@`json:"methodName"` } @H_403_29@func LoadConfig(path string,v @H_403_29@interface{}) { bytes,err := IoUtil.ReadFile(path) @H_403_29@if err != nil { fmt.Printf(@H_403_29@"Failed to open config file '%s': %s\n",path,err) @H_403_29@return @H_403_29@ } err = json.Unmarshal(bytes,v) @H_403_29@if err != nil { fmt.Println(@H_403_29@"Unmarshal: ",err.Error()) @H_403_29@return @H_403_29@ } }
system.json
{ @H_403_29@"objList": [ { @H_403_29@"objType": 3,@H_403_29@"ruleClassName": @H_403_29@"Adsl",@H_403_29@"serviceType": 1 },{ @H_403_29@"objType": 4,@H_403_29@"ruleClassName": @H_403_29@"Lan",@H_403_29@"serviceType": 2 } ],@H_403_29@"optList": [ { @H_403_29@"optType": 1,@H_403_29@"methodName": @H_403_29@"Add" @H_403_29@ },{ @H_403_29@"optType": 2,@H_403_29@"methodName": @H_403_29@"Del" } ] }