Warning
go doesn't support dlclose, so you need to reset the state every time the module is opened golang/go#11100 (comment) this means that all global variables will not reset, so you need to reset them manually on gmod13_open
Note
If you are using an editor, you may have lots of errors, this is due to how cgo works. You can ignore these errors, as they will not affect the final build.
You can use _example/go_build.py
to build your module and it will just work. I asumme you have python installed.
Note
Previously, I suggested using MySys2 to install, however I have found that it causes crashes when restarting the server. I recommend using WinLibs MinGW-w64 instead.
-
Download WinLibs MinGW-w64:
- Visit the WinLibs website and download the latest standalone MinGW-w64 builds:
- 64-bit GCC: Download the
mingw64
archive. - 32-bit GCC: Download the
mingw32
archive.
- 64-bit GCC: Download the
- Visit the WinLibs website and download the latest standalone MinGW-w64 builds:
-
Extract the Archives:
- Extract each
.zip
archive to:C:\mingw64
.C:\mingw32
.
- Extract each
-
Add MinGW-w64 Binaries to Your PATH:
- Steps to Add to PATH:
- Press
Win + S
, type Environment Variables, and select Edit the system environment variables. - In the System Properties window, click on the Environment Variables... button.
- Under System variables, find and select the
Path
variable, then click Edit. - Click New and add both paths:
C:\mingw64\bin
C:\mingw32\bin
- Click OK to save the changes.
- Press
- Steps to Add to PATH:
First, update your package list and install the required GCC packages:
sudo apt-get update
sudo apt-get install gcc-i686-linux-gnu
sudo apt-get install gcc-multilib
Once the necessary GCC setup is complete, you can install glua
using the following command:
go get -u github.com/Srlion/glua
package main
import (
"github.com/Srlion/glua"
)
func init() {
glua.GMOD13_OPEN = gmod13_open
glua.GMOD13_CLOSE = gmod13_close
}
func test(L glua.State) int {
var str string = L.CheckString(1)
println("string:", str)
L.PushString("Hello from Go!")
return 1
}
func gmod13_open(L glua.State) int {
println("hello from gmod13_open!")
L.PushGoFunc(test)
L.SetGlobal("test")
return 0
}
func gmod13_close(L glua.State) int {
return 0
}
// Required by Go when using `-buildmode=c-shared`
func main() {}