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