首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Go文件转JS失败

Go文件转JS失败
EN

Stack Overflow用户
提问于 2020-04-08 06:50:32
回答 1查看 566关注 0票数 0

我尝试使用https://github.com/sagiegurari/node-go-requirehttps://github.com/gopherjs将go文件作为模块运行

我的go代码中没有任何错误,但当使用下面的JS时,我遇到了“Error: Failed to convert Go file to JS”。

下面是我的nodeJS代码:

代码语言:javascript
复制
    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);

     }

下面是我的go主文件:

代码语言:javascript
复制
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函数:

代码语言:javascript
复制
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(),
    })
}

如果有人能帮上忙,我将不胜感激,因为我几乎是新手。

下面是我为上下文所做的工作:https://github.com/OliverBrotchie/phantom

EN

回答 1

Stack Overflow用户

发布于 2020-04-08 15:15:31

您应该手动运行gopherjs以查看实际错误:例如:

代码语言:javascript
复制
/workspace/go/bin/gopherjs  build /workspace/phantom/source/cmd/phantom.go

例如,一个问题可能是go运行时太旧或太新。一旦你看到这个问题,下一个需要帮助的地方很可能是https://github.com/gopherjs/gopherjs上的gopherjs github项目页面,在那里你可以直接打开问题。

由于no- go -require只是它的一个代理(基本上是为了支持go文件的无缝节点请求功能),所以我不会试图找出问题所在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61090649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档