GopherLua

package module
v1.0.0-alpha1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2020 License: Apache-2.0 Imports: 1 Imported by: 0

README

GopherLua 🚜

lua为go增加动态化能力,go为lua提供功能拓展

项目依托gopher-luago平台的lua解释器 对lua进行拓展

go拓展库

  • 数据库连接查询库db_module
  • 网络请求库httpRequest
安装--Installation
go get github.com/lisabiya/GopherLua
简单示例-http请求 (simple Example httpRequest)
func httpSimpleTest() {
	//
	gopherLua := GopherLua.NewState()
	gopherLua.Register(module_http.ModuleHttp{})
	err := gopherLua.DoString(
`
    --引用声明模块
	local http = httpRequest.new()
    local code, response =  http:End({
        get = "https://www.wanandroid.com/hotkey/json",
        query = "nihao",
    })
    --调用函数  
    print(code,response)
`)
	if err != nil {
		fmt.Println(err.Error())
	}
}



添加自定义拓展

主要是提供一种思路,需要优化改进的地方还有很多

  • 参考 module_http,module_db

  • 示例
  • go声明元表函数

const metatableName = "request_metatable"

func RegisterType(L *lua.LState) {
	mt := L.NewTypeMetatable(metatableName)
    //声明全局对象
	L.SetGlobal("httprequest", mt)
    //添加拓展函数
	L.SetField(mt, "get", L.NewFunction(getSimple))
}

func getSimple(L *lua.LState) int {
	var request = L.CheckTable(1)
	var requestMap, ok = goTool.TransLuaValue2Map(request).(map[string]interface{})
	if ok {
		_, body, _ := gorequest.New().
			Get(requestMap["url"].(string)).
			Query(requestMap["query"]).End()
		L.Push(lua.LNumber(0))
		L.Push(lua.LString(body))
		return 2
	} else {
		L.Push(lua.LNumber(1))
		L.Push(lua.LString("参数转map失败"))
		return 2

	}
}
  • lua中调用
  --直接引用声明模块
    local code, response = httprequest:get(
            { url = "https://www.wanandroid.com/hotkey/json" })
  --调用函数  
    print(code,response)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InterfaceModule

type InterfaceModule interface {
	RegisterType(L *lua.LState)
	Close()
}

type Lua

type Lua struct {
	State *lua.LState
	// contains filtered or unexported fields
}

func NewState

func NewState() *Lua

func (*Lua) Close

func (instance *Lua) Close()

func (*Lua) DoFile

func (instance *Lua) DoFile(path string) error

func (*Lua) DoString

func (instance *Lua) DoString(source string) error

func (*Lua) ExecuteFunc

func (instance *Lua) ExecuteFunc(funcName string, returnParamsCount int, args ...lua.LValue) error

func (*Lua) Register

func (instance *Lua) Register(module ...InterfaceModule)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL