加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 19:10:36 所属栏目:大数据 来源:网络整理
导读:目录结构: I:workspace_golandhellotree /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 (

目录结构:

I:workspace_golandhello>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': %sn",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" }
  ]
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读