我正在尝试将 go 文件作为模块运行:https://github.com/sagiegurari/node-go-require和 https://github.com/gopherjs
我的 go 代码没有任何错误,但是在使用下面的 JS 时,我遇到了“错误:无法将 Go 文件转换为 JS”
这是我的nodeJS代码:
require('node-go-require');
//Failing here
const Phantom = require(__dirname + '/source/cmd/phantom.go').phantom;
var params = {
server:null,
boundIP:null,
boundPort:null,
timeOut:null
}
//Some other code here to set params
function start(){
//determine which parameters have been set by user
var args = [params.server];
if(params.boundIP != null){
args.push(params.boundIP);
} else {
args.push("0.0.0.0");
}
if(params.boundPort != null){
args.push(params.boundPort);
} else {
args.push(0);
}
if(params.timeOut != null){
args.push(params.timeOut);
} else {
args.push(60);
}
var p = Phantom.new(...args);
}
这是我的主要文件:
package main
import (
"github.com/gopherjs/gopherjs/js"
"github.com/jhead/phantom/internal/proxy"
)
func main() {
js.Module.Get("exports").Set("phantom", map[string]interface{}{
"new": proxy.New,
})
}
这是proxy.New函数:
package proxy
import (
"fmt"
"math/rand"
"net"
"time"
"github.com/gopherjs/gopherjs/js"
"github.com/jhead/phantom/internal/clientmap"
"github.com/jhead/phantom/internal/logging"
"github.com/jhead/phantom/internal/proto"
"github.com/tevino/abool"
reuse "github.com/libp2p/go-reuseport"
)
var idleCheckInterval = 5 * time.Second
type ProxyServer struct {
bindAddress *net.UDPAddr
remoteServerAddress *net.UDPAddr
pingServer net.PacketConn
server *net.UDPConn
clientMap *clientmap.ClientMap
prefs ProxyPrefs
dead *abool.AtomicBool
}
type ProxyPrefs struct {
BindAddress string
BindPort uint16
RemoteServer string
IdleTimeout time.Duration
}
func New(BindAddress string, BindPort uint16, RemoteServer string, IdleTimeout time.Duration) *js.Object {
var prefs = new(ProxyPrefs)
prefs.BindAddress = BindAddress
prefs.BindPort = BindPort
prefs.RemoteServer = RemoteServer
prefs.IdleTimeout = time.Duration(IdleTimeout) * time.Second
bindPort := prefs.BindPort
// Randomize port if not provided
if bindPort == 0 {
randSource := rand.NewSource(time.Now().UnixNano())
bindPort = (uint16(randSource.Int63()) % 14000) + 50000
}
// Format full bind address with port
prefs.BindAddress = fmt.Sprintf("%s:%d", prefs.BindAddress, bindPort)
bindAddress, err := net.ResolveUDPAddr("udp", prefs.BindAddress)
if err != nil {
return nil
}
remoteServerAddress, err := net.ResolveUDPAddr("udp", prefs.RemoteServer)
if err != nil {
return nil
}
return js.MakeWrapper(&ProxyServer{
bindAddress,
remoteServerAddress,
nil,
nil,
clientmap.New(prefs.IdleTimeout, idleCheckInterval),
*prefs,
abool.New(),
})
}
如果有人可以提供帮助,将不胜感激,因为我对 GO 几乎是新鲜的。
这是我正在处理的上下文:https://github.com/OliverBrotchie/phantom
最佳答案
您应该手动运行 gopherjs 以查看实际错误:
例如:
/workspace/go/bin/gopherjs build /workspace/phantom/source/cmd/phantom.go
例如,一个问题可能是 go 运行时太旧或太新。
一旦你看到这个问题,下一个寻求帮助的地方很可能是 gopherjs github 项目页面,地址为 https://github.com/gopherjs/gopherjs。
您可以在哪里直接打开问题。
因为 no-go-require 只是它的代理(基本上是为了启用 go 文件的无缝 Node 要求功能),所以我不会尝试在那里找到问题。
关于javascript - 将 Go 文件转换为 JS 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61090649/