tech

[English] [Japanese]

80.2 Packages when transpiling to Go

Modules that are packaged by go can be used from LuneScript.

package management overview for go

go manages packages with go.mod.

This go.mod -managed package is cached in the local directory specified by the environment variable GOPATH .

LuneScript can take advantage of this cached module.

To use the package on github with go, write as follows.

import "github.com/ifritJP/lnshttpd"

In the same sense, LuneScript can access go modules.

import

To import the .lns file in the go package with LuneScript, write as follows.

// @lnsFront: skip
import go/github:com.ifritJP.lnshttpd.lnsservlet;

This is an example of importing lnsservlet.lns from https://github.com/ifritJP/lnshttpd.

Now focus on go/github:com.ifritJP.lnshttpd.lnsservlet.

As you can imagine by comparing https://github.com/ifritJP/lnshttpd and go/github:com.ifritJP.lnshttpd.lnsservlet, there are the following additional rules when using the go package.

  • Prepend go/
  • If . is attached like github.com, specify : instead of .

LuneScript will search for modules in go's package path instead of the normal search path when given a module of the above form.

Specifically, it searches for files in the following order:

  • vendor/github.com/ifritJP/lnshttpd/lnsservlet.lns
  • ${GOPATH}/pkg/mod/github.com/ifrit!j!p/lnshttpd@XXXXXXXXXXXXXXXXX/lnsservlet.lns

Here, the XXXXXXXXXXXXXXXXX part of @XXXXXXXXXXXXXXXXX/lnsservlet.lns comes from the module's TAG information listed in go.mod. In other words, you need to do go mod tidy beforehand.

module

A module declaration is required to use go modules from LuneScript.

The module declaration is also used when using the Lua module from LuneScript, but there are the following additional rules when using the go module.

  • Add to the module declaration that you are using a go module

Below is a declaration example when using servlet.go of github.com.ifritJP.lnshttpd.

// @lnsFront: skip
module servlet require "go/github:com.ifritJP.lnshttpd.servlet" of "" {
   pub static fn Start( listenPort:int,
                        handlerList: &List<&lnsservlet.HandlerInfo>,
                        hostingList:&List<&lnsservlet.HostingInfo> );
}

Here, of "" of module servlet require "go/github:com.ifritJP.lnshttpd.servlet" of "" declares the type of module to be used.

The character strings that can be specified are as follows.

string module language
"" Same as transcompile target language
"Lua" lua
"Go" go

Available languages

There are restrictions on the combinations of languages that can be transcompiled and languages that can be used in modules.

go module lua module
transcompile to go Possible Possible
transcompile to lua impossibility Possible

type compatibility

You can use the go module from LuneScript with the module declaration.

However, type compatibility is limited.

The following types are paired in LuneScript and go.

Types in LuneScript type of go
int int
real float64
bool bool
str string
nilable interface{}