使用SAP的nwrfcsdk和gorfc库。
基本步骤如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
Windows下使用go连接SAP RFC函数 #安装nwrfcsdk C:\nwrfcsdk #配置path ;C:\nwrfcsdk\lib #配置环境变量 变量名:CGO_CFLAGS 变量值:C:\nwrfcsdk\include 变量名:CGO_LDFLAGS 变量值:C:\nwrfcsdk\lib #安装gcc https://sourceforge.net/projects/mingw/ #安装go saprfc库 go get github.com/stretchr/testify go get github.com/sap/gorfc cd $GOPATH/src/github.com/sap/gorfc/gorfc go build go install #Hello World package main import ( "fmt" "github.com/sap/gorfc/gorfc" "github.com/stretchr/testify/assert" "reflect" "testing" "time" ) func abapSystem() gorfc.ConnectionParameter { return gorfc.ConnectionParameter{ Dest: "I64", Client: "800", User: "demo", Passwd: "welcome", Lang: "EN", Ashost: "11.111.11.111", Sysnr: "00", Saprouter: "/H/222.22.222.22/S/2222/W/xxxxx/H/222.22.222.222/H/", } } func main() { c, _ := gorfc.Connection(abapSystem()) var t *testing.T params := map[string]interface{}{ "IMPORTSTRUCT": map[string]interface{}{ "RFCFLOAT": 1.23456789, "RFCCHAR1": "A", "RFCCHAR2": "BC", "RFCCHAR4": "ÄBC", "RFCINT1": 0xfe, "RFCINT2": 0x7ffe, "RFCINT4": 999999999, "RFCHEX3": []byte{255, 254, 253}, "RFCTIME": time.Now(), "RFCDATE": time.Now(), "RFCDATA1": "HELLÖ SÄP", "RFCDATA2": "DATA222", }, } r, _ := c.Call("STFC_STRUCTURE", params) assert.NotNil(t, r["ECHOSTRUCT"]) importStruct := params["IMPORTSTRUCT"].(map[string]interface{}) echoStruct := r["ECHOSTRUCT"].(map[string]interface{}) assert.Equal(t, importStruct["RFCFLOAT"], echoStruct["RFCFLOAT"]) assert.Equal(t, importStruct["RFCCHAR1"], echoStruct["RFCCHAR1"]) assert.Equal(t, importStruct["RFCCHAR2"], echoStruct["RFCCHAR2"]) assert.Equal(t, importStruct["RFCCHAR4"], echoStruct["RFCCHAR4"]) assert.Equal(t, importStruct["RFCINT1"], echoStruct["RFCINT1"]) assert.Equal(t, importStruct["RFCINT2"], echoStruct["RFCINT2"]) assert.Equal(t, importStruct["RFCINT4"], echoStruct["RFCINT4"]) // assert.Equal(t, importStruct["RFCHEX3"], echoStruct["RFCHEX3"]) assert.Equal(t, importStruct["RFCTIME"].(time.Time).Format("150405"), echoStruct["RFCTIME"].(time.Time).Format("15. assert.Equal(t, importStruct["RFCDATE"].(time.Time).Format("20060102"), e/Users/d037732/Downloads/gorfc/README.mdchoStruct["RFCDATE"].(time.Time).Format(". assert.Equal(t, importStruct["RFCDATA1"], echoStruct["RFCDATA1"]) assert.Equal(t, importStruct["RFCDATA2"], echoStruct["RFCDATA2"]) fmt.Println(reflect.TypeOf(importStruct["RFCDATE"])) fmt.Println(reflect.TypeOf(importStruct["RFCTIME"])) c.Close() |
本文作者: GavinDong
版权属于: GavinDong博客
文章链接: https://gavindong.com/2589.html
如果使用过程中遇到问题,可 **点击此处** 交流沟通。
版权所有,转载时必须以链接形式注明作者和原始出处及本声明。
评论列表(12条)
请教,我的也出现了同样的报错,也用了同样的gcc版本,依旧如此,我这是哪里配置错了呢,SDK和版本不兼容还是什么问题?能否发一份您能用的SDK版本文件和配置环境清单给我呢?谢谢了
Thread model: posix
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
G:\app\0\ABAP\workspace\src\0go\gorfc\gorfc>go build gorfc.go
# runtime/cgo
G:/app/vscode/.portable/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find G:\app\0\ABAP\SDK\nwrfcsdk\lib: Permission denied
collect2.exe: error: ld returned 1 exit status
# runtime/cgo
gcc: warning: G:\app\0\ABAP\SDK\nwrfcsdk\include: linker input file unused because linking not done
# runtime/cgo
gcc: warning: G:\app\0\ABAP\SDK\nwrfcsdk\include: linker input file unused because linking not done
# runtime/cgo
(后略)
windows下情况比较多,我现在也没有搭这个环境了。
建议linux虚拟机开发完部署就好了,linux下的gosaprfc可参考这篇文章:https://gavindong.com/2595.html
謝謝分享,另外請教一下
#安装go saprfc库,這一段若在Windows裡該如何處理? cmd無法執行export..等
還有執行go build跟go install都會出現如下錯誤,請問您有遇過嗎?!該如何解呢?
——-
# runtime/cgo
C:/minGW/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-
mingw32/bin/ld.exe: cannot find C:\nwrfcsdk\lib: Permission denied
collect2.exe: error: ld returned 1 exit status
# runtime/cgo
gcc: warning: C:\nwrfcsdk\include: linker input file unused because linking not
done
minGW64位:
http://www.mingw-w64.org/doku.php
MinGW-w64已经是超越了MinGW成为事实上的标准【此话出自英文维基MinGW条目】
minGW64位下载地址:
https://sourceforge.net/projects/mingw-w64/files/latest/download
安装时注意将Architecture选择为x86_64,其他的可以不变。
PATH添加gcc目录:
PATH应添加 ;C:\mingw64\bin
然后打开命令行,执行gcc -v,如果出现类似下面的信息,说明安装成功:
gcc version 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
还需添加一个环境变量:C_INCLUDE_PATH
变量值为:C:\mingw64\include
如果go build的时候报以下错误,说明安装的MinGW可能安装成32位了,卸载重新安装64位即可(可以通过gcc -v查看gcc的版本):
# runtime/cgo
cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
补充事项两点:
1.PATH需要添加gcc的路径,例如我安装的是MinGW64,安装在C盘,则PATH应添加 ;C:\mingw64\bin
2.go get github.com/sap/gorfc的时候老是报错,索性就直接cd到$GOPATH/src下面,使用git clone https://github.com/SAP/gorfc.git
配置PATH之后要关闭cmd重新开一次,PATH才会生效的。