我目前正在通过SWIG Go examples工作,第二个示例“ constants”有问题
出于某种原因,即时消息超出了范围,只能在我以swig调用方式指定32
作为-intgosize
参数的参数时使用。
如果我用swig -go -intgosize 64 example.i
痛饮,一切正常
example.i
swig文件如下所示
/* File : example.i */
%module example
/* A few preprocessor macros */
#define ICONST 42
#define FCONST 2.1828
#define CCONST 'x'
#define CCONST2 '\n'
#define SCONST "Hello World"
#define SCONST2 "\"Hello World\""
/* This should work just fine */
#define EXPR ICONST + 3*(FCONST)
/* This shouldn't do anything */
#define EXTERN extern
/* Neither should this (BAR isn't defined) */
#define FOO (ICONST + BAR)
/* The following directives also produce constants */
%constant int iconst = 37;
%constant double fconst = 3.14;
从使用32参数运行swig时得到的输出判断,我认为恐慌是由SCONST2
引起的,在注释掉#define SCONST2
行之后,恐慌就消失了。
SCONST2
的输出如下所示:
panic: runtime error: slice bounds out of range [:824633720845] with length 2147483647
goroutine 1 [running]:
swigtest/02_constants.swigCopyString(0x1056760, 0xc00000000d, 0xc000107ef0, 0xc000107ef8)
C:/Users/visfgp/go/personal_code/src/swigtest/02_constants/example.go:64 +0xb0
swigtest/02_constants._swig_getSCONST2(0x50230a, 0xc00003e1e0)
C:/Users/visfgp/go/personal_code/src/swigtest/02_constants/example.go:97 +0x4a
swigtest/02_constants.init()
C:/Users/visfgp/go/personal_code/src/swigtest/02_constants/example.go:101 +0x38
我的问题是,此字符串会引发这种恐慌,为什么SCONST
字符串不会引发这种恐慌?它与go ints的大小有什么关系?
答案 0 :(得分:0)
定义int32变量时,其范围是-2147483648
至2147483647
。也许您的变量超出了此范围[:824633720845]
。在这种情况下,您应该使用int64!