GoLang Call Python

This example shows how to use starcore_for_go, which enable go language to call python.

Background

This example shows how to use starcore_for_go, which enable go language to call python, including calling python functions, creating instances of python classes, getting and setting Python variables, capturing python printouts, and setting python callbacks, etc.

1. Install

  • For windows, python and cle for win32 must be installed.

  • For linux, python and cle for linux must be installed.

  • Install starfore_for_go

    go get github.com/srplab/starcore_for_go/stargo
    

Using the code

1. Init cle using starcore_for_go

    import (
      	"fmt"
       	"github.com/srplab/starcore_for_go/stargo"
    )

    func main() {
       	Service := stargo.InitSimple("test", "123", 0, 0)
       	SrvGroup := Service.Get("_ServiceGroup").(*stargo.StarSrvGroup)
       	Service.CheckPassword(false)
    }
  

2. Loading and initializing python

    SrvGroup.InitRaw("python", Service)
  • Load python2.7 by default, you can use python35, python36, etc. to load different versions

  • Get python global context, with it, app can get or set python global variables, classes or functions.

    python := Service.ImportRawContext("python", "", false, "")

3. Set callbacks to capture output from python

  • Callback function
    func MsgCallBack(ServiceGroupID uint32, uMsg uint32, wParam interface{}, lParam interface{}) (IsProcessed bool, Result interface{}) {
    	if uMsg == stargo.MSG_VSDISPMSG || uMsg == stargo.MSG_VSDISPLUAMSG || uMsg == stargo.MSG_DISPMSG || uMsg == stargo.MSG_DISPLUAMSG {
		    fmt.Println(wParam)
    	} else {
		    fmt.Println(ServiceGroupID, uMsg, wParam, lParam)
    	}
    	return false, 0
    }
  • Set Callback function
    func main() {
    	Service := stargo.InitSimple("test", "123", 0, 0)
    	SrvGroup := Service.Get("_ServiceGroup").(*stargo.StarSrvGroup)
    	Service.CheckPassword(false)

    	stargo.RegMsgCallBack_P(MsgCallBack)

        ...
    }

4. Interoperation with python

  • Run python file
    Service.DoFile("python", "testpy.py", "")
  • Get python global variable
    fmt.Println(python.Get("g1"))
  • Python class related operations
    //Get python class
    Multiply := Service.ImportRawContext("python", "Multiply", true, "")
    //Create instance of python class
    multiply := Multiply.New("", "", 33, 44)
    //Invoke function of python instance
    fmt.Printf("instance multiply = %d\n", multiply.Call("multiply", 11, 22))

5. Set callback to python

    Service.DoFile("python", " testcallback.py", "")

    CallBackObj := Service.New()
    python.Set("CallBackObj", CallBackObj)

    CallBackObj.RegScriptProc_P("PrintHello", func(CleGroup *stargo.StarSrvGroup, CleService *stargo.StarService, CleObject *stargo.StarObject, Paras []interface{}) interface{} {
        fmt.Println(Paras)
        return []interface{}{"return from go", 345.4}
    })

    retobj := python.Call("TestCallBack", "hello ", "world").(*stargo.StarObject)
    fmt.Println(retobj)

6. Run

  go build
  
  load library error[126], try[C:\srplab/libs64/star_python36.dll]
  load library error[126], try[C:\WINDOWS\system32/star_python36.dll]
  load library error[126], try[C:\srplab/libs64/star_python36.pyd]
  load library [C:\srplab/libs64/star_python36.pyd] success....
  basic script [python] for version [36] is registered, you can use [python] to interact with the script
  123
  multiply.... <__main__.Multiply object at 0x0000000006D69320> 11 22
  instance multiply = 242
  hello  world
  [--------------- 1234]
  return from go
  345.4
  Name60E15194[python36:tuple](666, 777)  
  • python code of “testpy.py”
def tt(a,b) :
    print(a,b)
    return 666,777
g1 = 123
def yy(a,b,z) :
    print(a,b,z)
    return {'jack': 4098, 'sape': 4139}

class Multiply :
    def __init__(self,x,y) :
        self.a = x
        self.b = y
    
    def multiply(self,a,b):
        print("multiply....",self,a,b)
        return a * b
  • python code of “testcallback.py”
def TestCallBack(a,b) :
    print(a,b)
    ParaPkg = CallBackObj.PrintHello("---------------",1234)
    print(ParaPkg[0])
    print(ParaPkg[1])
    return 666,777

Sample Download

go_call_python.zip