通过Node调用golang函数时无法打开文件

时间:2018-11-20 15:14:57

标签: node.js go

我按照https://medium.com/learning-the-go-programming-language/calling-go-functions-from-other-languages-4c7d8bcc69bf中的教程进行操作,以使我的节点应用程序能够调用golang函数。所提供的示例就像吊饰一样。
但是,我确实无法在另一种情况下实现它。在这里,我只想通过提供其绝对路径来打开文件并调用Go的函数,但是它总是告诉我由于没有这样的文件而找不到该文件。我试图直接在Go中运行它,并且可以运行!
是我做错了还是真正的错误/未完成的功能?

这是我为c风格的lib构建的golang源码:

package main

import "C"

import (
    "bufio"
    "fmt"
    "log"
    "os"
)

func main() {}

//export ReadSomething
func ReadSomething(filePath string) {
    file, err := os.Open(filePath)
    if err != nil {
        log.Fatal(err)
    }

    defer file.Close()

    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }

}

我用以下命令构建它:

go build -buildmode=c-shared -o simpleread.so main.go

如果您想知道标头输出是什么:

/* Created by "go tool cgo" - DO NOT EDIT. */

/* package command-line-arguments */

#line 1 "cgo-builtin-prolog"

#include <stddef.h> /* for ptrdiff_t below */

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

typedef struct { const char *p; ptrdiff_t n; } _GoString_;

#endif

/* Start of preamble from import "C" comments.  */
/* End of preamble from import "C" comments.  */


/* Start of boilerplate cgo prologue.  */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef __SIZE_TYPE__ GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

typedef _GoString_ GoString;
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif


extern void ReadSomething(GoString p0);

#ifdef __cplusplus
}
#endif

然后,下面是我如何通过Node称呼它。我在产生错误的行上给出评论:

var ref = require("ref")
var ffi = require("ffi-napi")
var Struct = require("ref-struct")
var ArrayType = require("ref-array")
var LongArray = ArrayType(ref.types.longlong);

  var GoString = Struct({
    p: "string",
    n: "longlong"
  });

  var simpleRead = ffi.Library("./simpleread.so", {
    ReadSomething: ["void", [GoString]]
  });

  // error here, can't open the specified file
  simpleRead.ReadSomething("/home/ivan/Documents/crashsite/node-go-crossfire/simpletext.txt")

我正在Ubuntu 18.04 64bit上运行它。

1 个答案:

答案 0 :(得分:1)

请记住,Go中的字符串就像切片一样。它们由指向备份数据和长度的指针组成。这就是为什么在您的代码中,Exception in thread "main" java.lang.ClassCastException: java.base/jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.base/java.net.URLClassLoader at monitor.Main.logClassPathContent(Main.java:46) at monitor.Main.main(Main.java:28) 被定义为:

URLClassLoader

我建议您定义一个用于创建GoString的函数,例如

var GoString = Struct({
    p: "string",  // pointer
    n: "longlong" // length
});

您可以在代码中使用以下代码:

GoString