SAP / gorfc

SAP NW RFC Connector for GO
Apache License 2.0
99 stars 43 forks source link
golang netweaver nwrfc open-source rfc-connector sap

SAP NetWeawer RFC SDK client bindings for GO

For more details on the SAP NetWeaver Remote Function Call (RFC) Software Development Kit (SDK) please see its support page.

license REUSE status Go Report Card GoDoc

Call for Maintainers

This project is currently looking for new maintainers. Please see this issue for details.

Features

Supported Platforms

Requirements

All platforms

Windows

macOS

$ xcode-select --install
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setstealthmode off

SPJ articles

Highly recommended reading about RFC communication and SAP NW RFC Library, published in the SAP Professional Journal (SPJ)

Installation

To start using SAP NW RFC Connector for Go, you shall:

  1. Install and Configure Go
  2. Install the SAP NW RFC Library for your platform
  3. Install the GORFC package

Install and Configure Go

If you are new to Go, the Go distribution shall be installed first, following GO Installation and GO Configuration instructions. See also GO Environment Variables.

Windows Config Example

After running the MSI installer, the default C:\Go folder is created and the GOROOT system variable is set to C:\Go.

Create the Go work environment directory:

cd c:\
mkdir workspace

Set the environment user varialbes GOPATH and GOBIN, add the bin subdirectories to PATH and restart the Windows shell.

GOPATH = C:\workspace
GOBIN = %GOPATH%\bin
PATH = %GOROOT%\bin;%GOBIN%:%PATH%

See also GO on Windows Example.

Linux

The work environment setup works the same way like on Windows and these instructions describe the installation on Ubuntu Linux for example.

Install SAP NW RFC Library

To obtain and install SAP NW RFC Library from SAP Service Marketplace, you can follow the same instructions as for Python or nodejs RFC connectors.

Install GORFC

To install gorfc and dependencies, run following commands:

export CGO_CFLAGS="-I $SAPNWRFC_HOME/include"
export CGO_LDFLAGS="-L $SAPNWRFC_HOME/lib"
export CGO_CFLAGS_ALLOW=.*
export CGO_LDFLAGS_ALLOW=.*
go get github.com/stretchr/testify
go get github.com/sap/gorfc
cd $GOPATH/src/github.com/sap/gorfc/gorfc
go build
go install

To test the installation, run the example provided:

cd $GOPATH/src/github.com/sap/gorfc/example
go run hello_gorfc.go

Getting Started

See the _hellogorfc.go example and _gorfctest.go unit tests.

The GO RFC Connector follows the same principles and the implementation model of Python and nodejs RFC connectors and you may check examples and documentation there as well.

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()

Licensing

Please see our LICENSE file for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available via the REUSE tool.

References