Golang binding Lua script, small package

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Well, I admit I'm a little bored!


Originally, I wrote a small in C, the integration of Lua script, Lua is mainly used to configure the system, feel very good.


Recently toss a time of Golang, write code feeling, indeed better than C, so think with Golang write a crawl novel of a slightly generic crawler, basic function, found the configuration or LUA more convenient, then try to encapsulate the LUA C-API bindings, the whole half a day, Found quite tired, went to GitHub search, found a few predecessors wrote East, but feel not very satisfied, finally through Golang official website found this.


Well, Github.com/aarzilli/golua, this package is relatively complete. Trial, and C-api basically consistent, the problem is, from the configuration file inside take a thing, still and c-api like, cumbersome. Hey, then you can only wrap it yourself again. The whole 2 hours, it feels good, read the basic configuration no problem. First put it out to see, and then slowly optimize it!


File:test.lua

w = 100h = 100.2str= "Hello World" TB = {1,2,3,4,sd={n= "Twkun", p= "password"},v= "value", list={1,2,4,5,6,7,8}}

FIle:test.go


Summary:
Package Mainimport ("./luawrapper"//"Twkun/tool/luawrapper" "FMT") func main () {lua: = new (Luawrap.luawrap) Lua. Init () Lua. Dofile ("Test.lua") defer lua. Close () fmt. PRINTLN (LUA). Getinteger ("W")) Fmt. PRINTLN (LUA). GetNumber ("H")) Fmt. PRINTLN (LUA). GetData ("H").    (float64)) Fmt. PRINTLN (LUA). GetString ("str")) var tb luawrap.iluatable = Lua. GetTable ("TB") fmt. PRINTLN (TB. GetString ("V")) tf: = tb. GetTable ("SD")//TF: = tb. GetData ("SD"). (luawrap.iluatable)//ok FMT. PRINTLN (TF. GetString ("n")) Fmt. PRINTLN (TF. GetData ("n").    (string)) Fmt. PRINTLN (TB. GetTable ("SD"). GetString ("P")) FMT. PRINTLN (TB. GetTable ("SD"). GetData ("P").    (string)) N: = tb. GetSize () fmt. PRINTLN (n) for I: = 1;i<= n;i++ {fmt. PRINTLN (TB. GetIndex (i).    (float64)) } FMT. Println () tf = tb. GetTable ("list") n = tf. GetSize () fmt. PRINTLN (n, "\ n") for I: = 1;i<= n;i++ {fmt. PRINTLN (TF. GetIndex (i).    (float64)) }}

Test code, write casually, name that poor Ah, later to pay attention to! You'll have time to tidy up and get on GitHub sometime.


Feel good, hey, weekend Annual meeting, no language, and go to the lake, incomparable ~ ~ ~

Create by Twkun
2013-12-13

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.