maps

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Code generated by gobindlua; DO NOT EDIT.

Code generated by gobindlua; DO NOT EDIT.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type User

type User struct {
	Name  string
	Age   int
	Email string
}

func NewUser

func NewUser(name string, age int, email string) User

func (*User) LuaMetatableType

func (r *User) LuaMetatableType() string

func (User) RegisterLuaType

func (goType User) RegisterLuaType(L *lua.LState)

type UserDatabase

type UserDatabase struct {
	Users map[int]User
}
Example
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/ChrisTrenkamp/gobindlua"
	lua "github.com/yuin/gopher-lua"
)

const script = `
--[[ Just like slices, you can use tables to construct Go maps. ]]
local user1 = user.new("Mike Smith", 42, "mike.smith@example.com")
local user2 = user.new("Ryan Kennedy", 23, "rkennedy04021@nyu.com")
local user3 = user.new("Robert Rose", 70, "rrose00011@aol.com")
local db = user_database.new_from(
	{
		[10]=user1,
		[11]=user2,
		[12]=user3,
	}
)
user_db.users = db.users
`

func main() {
	L := lua.NewState()
	defer L.Close()

	User{}.RegisterLuaType(L)
	UserDatabase{}.RegisterLuaType(L)
	gobindlua.RegisterLuaMap(L)

	user_db := UserDatabase{}
	L.SetGlobal("user_db", gobindlua.NewUserData(&user_db, L))

	if err := L.DoString(script); err != nil {
		log.Fatal(err)
	}

	jsonBytes, err := json.MarshalIndent(user_db.Users, "", "\t")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(jsonBytes))

}
Output:

{
	"10": {
		"Name": "Mike Smith",
		"Age": 42,
		"Email": "mike.smith@example.com"
	},
	"11": {
		"Name": "Ryan Kennedy",
		"Age": 23,
		"Email": "rkennedy04021@nyu.com"
	},
	"12": {
		"Name": "Robert Rose",
		"Age": 70,
		"Email": "rrose00011@aol.com"
	}
}

func NewUserDatabase

func NewUserDatabase() UserDatabase

func NewUserDatabaseFrom

func NewUserDatabaseFrom(users map[int]User) UserDatabase

func (*UserDatabase) LuaMetatableType

func (r *UserDatabase) LuaMetatableType() string

func (UserDatabase) RegisterLuaType

func (goType UserDatabase) RegisterLuaType(L *lua.LState)

Jump to

Keyboard shortcuts

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