golang按请求中的不同标识处理不同的业务逻辑

前端之家收集整理的这篇文章主要介绍了golang按请求中的不同标识处理不同的业务逻辑前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

目录结构:

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

  1. package main
  2.  
  3. import (
  4. "com/bo" "com/conf" "fmt" "reflect" "strconv" "strings" )
  5.  
  6. var regStruct map[string]interface{}
  7. var sys conf.System
  8.  
  9. func init() {
  10.  
  11. conf.LoadConfig("I:/workspace_goland/hello/src/system.json",&sys)
  12.  
  13. regStruct = make(map[string]interface{})
  14. regStruct["Adsl"] = bo.Adsl{}
  15. regStruct["Lan"] = bo.Lan{}
  16.  
  17. }
  18.  
  19. //golang按请求中的不同标识处理不同的业务逻辑 func main() {
  20.  
  21. request := "3|||1|||P01=1|||M02=3" doRequest(request,sys)
  22.  
  23. request = "4|||2|||P01=1|||M02=3" doRequest(request,sys)
  24. }
  25.  
  26. func doRequest(request string,sys conf.System) {
  27. //解析请求,获取业务对象,操作对象和其他数据 fmt.Println("request: ",request)
  28. fields := strings.Split(request,"|||")
  29. objtype,_ := strconv.Atoi(fields[0])
  30. opttype,_ := strconv.Atoi(fields[1])
  31. ruleClassName := getRuleClassName(sys,objtype)
  32. methodName := getOptMethodName(sys,opttype)
  33.  
  34. execute(ruleClassName,methodName)
  35. }
  36.  
  37. //通过反射调用对象的操作方法 func execute(ruleClassName string,methodName string) {
  38. t := reflect.TypeOf(regStruct[ruleClassName])
  39. response := reflect.New(t).MethodByName(methodName).Call(nil)
  40. fmt.Println("response: ",response)
  41. fmt.Println()
  42. }
  43.  
  44. func getOptMethodName(sys conf.System,opttype int) string {
  45. var ret = "" optList := sys.OptList
  46. for _,obj := range optList {
  47. if opttype == obj.OptType {
  48. ret = obj.MethodName
  49. fmt.Println("methodName: ",ret)
  50. break }
  51. }
  52. return ret
  53. }
  54.  
  55. func getRuleClassName(sys conf.System,objtype int) string {
  56. var ret = "" objList := sys.ObjList
  57. for _,obj := range objList {
  58. if objtype == obj.ObjType {
  59. ret = obj.RuleClassName
  60. fmt.Println("RuleClassName: ",ret)
  61. break }
  62. }
  63. return ret
  64. }
  1.  

adsl.go

  1. package bo
  2.  
  3. type Adsl struct {
  4. }
  5.  
  6. func (obj Adsl) Add() string {
  7. ret := "invoke adsl add,then return data" return ret
  8. }
  9.  
  10. func (obj Adsl) Del() string {
  11. ret := "invoke adsl delete,then return data" return ret
  12. }
  1.  

lan.go

  1. package bo
  2.  
  3. type Lan struct {
  4. }
  5.  
  6. func (obj Lan) Add() string {
  7. ret := "invoke lan add,then return data" return ret
  8. }
  9.  
  10. func (obj Lan) Del() string {
  11. ret := "invoke lan delete,then return data" return ret
  12. }


system.go

  1. package conf
  2.  
  3. import (
  4. "encoding/json" "fmt" "io/IoUtil" )
  5.  
  6. type System struct {
  7. ObjList []obj `json:"objList"` OptList []opt `json:"optList"` }
  8.  
  9. type obj struct {
  10. ObjType int `json:"objType"` //变量首字母必须大写,不然解析不出来数据 RuleClassName string `json:"ruleClassName"` ServiceType int `json:"serviceType"` }
  11.  
  12. type opt struct {
  13. OptType int `json:"optType"` MethodName string `json:"methodName"` }
  14.  
  15. func LoadConfig(path string,v interface{}) {
  16. bytes,err := IoUtil.ReadFile(path)
  17. if err != nil {
  18. fmt.Printf("Failed to open config file '%s': %s\n",path,err)
  19. return }
  20. err = json.Unmarshal(bytes,v)
  21. if err != nil {
  22. fmt.Println("Unmarshal: ",err.Error())
  23. return }
  24. }
  1.  

system.json

  1. {
  2. "objList": [
  3. {
  4. "objType": 3,"ruleClassName": "Adsl","serviceType": 1
  5. },{
  6. "objType": 4,"ruleClassName": "Lan","serviceType": 2
  7. }
  8. ],"optList": [
  9. {
  10. "optType": 1,"methodName": "Add" },{
  11. "optType": 2,"methodName": "Del" }
  12. ]
  13. }

猜你在找的Go相关文章