登录
首页 >  Golang >  Go问答

如何避免包依赖

来源:stackoverflow

时间:2024-04-07 14:36:35 362浏览 收藏

今天golang学习网给大家带来了《如何避免包依赖》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

问题内容

我有一个由 4 部分组成的项目:

  1. 网关(gateway/gateway.go)是一个知道如何与应用程序服务器通信并打开此连接通道的包。

  2. 跑步者(runner/runner.go)是主要的(go build -o runner/runner runner/runner.go)它加载并执行模块(使用反射我从模块运行函数)! p>

  3. 框架 (framework/framework.go) 实现调用网关的许多功能。

  4. 模块(又名 Go 中的插件)(modules/sample.go) (go build -buildmode plugin -omodules/sample.so ./modules/sample.go) 使用框架,执行客户逻辑!当init时我导出struct的reflect.Value,然后runner就可以运行这个struct的方法。

我希望运行程序实例化网关,框架获取此实例,而不在运行程序/框架之间创建依赖关系。

为什么?为了避免运行程序加载模块时出现 Go 错误“插件是使用不同版本的包构建的”! 如果我更新运行器(框架已更改),我将使旧模块失效。

我已经使用两种我不喜欢的方式来做到这一点:

  1. 使用上下文,但模块和框架的所有函数都需要接收参数上下文,然后框架提取网关。

  2. 只要让框架实例化网关,但运行程序就无法使用网关。


解决方案


go插件有很多令人头疼的问题,尤其是插件编译器版本必须与程序的编译器版本完全匹配。但这个例子是有效的。

runner/runner.go

package main

import (
  "context"
  "fmt"
  "os"
  "plugin"
  "reflect"

  "../gateway"
  "../dep"
)

var a *gateway.gateway

func main() {
  mymodule := os.args[1]

  if _, err := plugin.open(mymodule); err != nil {
    os.exit(1)
  }

  mod, err := dep.newmodule()
  if err != nil {
    os.exit(1)
  }

  a = gateway.newgw()
  ctx := context.withvalue(context.background(), "gateway", a)

  modreflect, err := mod.init(ctx, dep.config{})
  if err != nil {
    os.exit(1)
  }

  if !modreflect.isvalid() {
    os.exit(1)
  }

  modnode, err := mod.start(ctx)
  if err != nil {
    os.exit(1002)
  }

  for {
    if len(modnode) <= 0 {
      break
    }

    modnoderefl := modreflect.methodbyname(modnode)
    if !modnoderefl.isvalid() {
      break
    }

    result := modnoderefl.call([]reflect.value{reflect.valueof(ctx)})
    if len(result) != 2 {
      break
    }

    modnode = result[0].string()
  }

  mod.end(ctx)
}

gateway/gateway.go

package gateway

type gateway struct {}

fun newgw() *gateway {
  a := gateway{}
  return &a
}

dep/dep.go

package dep

import (
  "errors"
  "context"
  "reflect"
)

// config is a configuration provider.
type config map[string]interface{}

// module is the interface implementated by types that
// register themselves as modular plug-ins.
type module interface {
  init(ctx context.context, config config) (reflect.value,error)
  start(ctx context.context) (string,error)
  end(ctx context.context) error
}

var themod = []func() module{}

func registermodule(ctor func() module) {
  themod = append(themod, ctor)
}

func newmodule() (module, error) {
  if len(themod) == 0 {
    return nil, errors.new("module not registered")
  }
  return themod[0](), nil
}

framework/framework.go

package framework

import (
    "fmt"
  "context"
  "../gateway"
)

type playfileinput struct {
  path string
}

func play(ctx context.context, p playfileinput) error {
  if a := ctx.value("gateway"); a != nil {

    if a.(*gateway.gateway) != nil {
      _, err := a.(*gateway.gateway).exec("playback", p.path)
      return err
    }
  }

  return nil
}

modules/sample.go

package main

import "C"

import (
    "context"
    "fmt"
    "os"
  "reflect"

  "../dep"
  "../framework"
)

type MyModuleImpl struct {}

func init() {
  dep.RegisterModule(func() dep.Module {
    return &MyModuleImpl{}
  })
}

func (m *MyModuleImpl) Init(ctx context.Context, config dep.Config) (reflect.Value, error) {
  return reflect.ValueOf(m), nil
}

func (m *MyModuleImpl) Start(ctx context.Context) (string,error) {
  return "Menu_1",nil
}
func (n *MyModuleImpl)Menu_1(ctx context.Context) (string, error) {
  framework.Play(ctx, framework.PlayFileInput{Path: "welcome.wav"})
  return "Menu_2",nil
}
func (n *MyModuleImpl)Menu_2(ctx context.Context) (string, error) {
  return "Menu_3", nil
}
// ....
// ....
func (m *MyModuleImpl) End(ctx context.Context) error {
  return nil
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何避免包依赖》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>