For some reason, the Python library is not easy to implement with Golang refactoring, so we can do this by calling the Python library via Go

  • Call Python as a shell command assembly

It mainly uses the GO OS /exec package and python’s -c arguments

func main(point int) {
	var err error
	var cmd *exec.Cmd
	var datas []byte
	py := "python"
	flag := "-c"
	importmodule := "import sys"
	Target := "print(sys.path)"
	params := fmt.Sprintf("%s; %s", importmodule, Target)
	fmt.Println(params)
	cmd = exec.Command(py, flag, params)
	ifdatas, err = cmd.Output(); err ! =nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println(string(datas))

}
}
Copy the code

Similarly, when using other self-written Python libraries, add the Python code path to the sys.path path before making the call.