Help us shape the future of Go! Take the 2026 Go Developer Survey

convert

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 3 Imported by: 0

README

Lua ↔ Go 深度值转换

convert 提供 Go 与 Lua 值的双向深度递归转换,供宿主层在 VM 边界交换嵌套数据结构(本模块 host 桥与宿主业务代码共用)。

API

Go → Lua
func ToLuaValue(L *lua.LState, val any) lua.LValue
  • 已是 lua.LValue 的值原样返回;
  • 基础类型(bool / string / 各整型 / float32/64)直转;
  • map[string]any / []any 递归建表;
  • 其余复杂类型(struct、其他 map/slice)走反射兜底。
Lua → Go
func ToGoValue(val lua.LValue) any
  • LTString → string、LTBool → bool、LTNil → nil、LTNumber → float64;
  • LTTable 按结构判定为 map[string]any 或 []any,深度递归。
便捷标量

ToString / ToNumber / ToBool——对常见标量做宽容取值。

注意

Lua 5.1 的 number 一律是 float64:从 Lua 取回的"整数"需要自行判断/取整后再交给期望 int 的 Go 接口。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBool

func ToBool(val lua.LValue) bool

ToBool converts a Lua value to a Go bool. Uses Lua's boolean semantics (only nil and false are false).

func ToGoValue

func ToGoValue(val lua.LValue) any

ToGoValue converts a Lua value to the best associated Go type. This is the primary function for Lua → Go conversion.

Based on dragon-mud's AsRaw pattern: - LTString → string - LTBool → bool - LTNil → nil - LTNumber → float64 - LTTable → map[string]any or []any (based on structure)

func ToLuaValue

func ToLuaValue(L *lua.LState, val any) lua.LValue

ToLuaValue converts a Go value to a Lua value. This is the primary function for Go → Lua conversion. Supports primitives, maps, slices, and nested structures.

Inspired by dragon-mud's getLValue and ValueFor pattern.

func ToNumber

func ToNumber(val lua.LValue) float64

ToNumber converts a Lua value to a Go float64. Uses Lua's built-in number conversion rules.

func ToString

func ToString(val lua.LValue) string

ToString converts a Lua value to a Go string. Uses Lua's built-in string conversion rules.

Types

This section is empty.

Jump to

Keyboard shortcuts

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