purpose

Python calls the Go method. Python has many powerful and concise libraries. The new Army Go’s multi-core utilization is also very strong. Of course, this is an obvious advantage. There are plenty of reasons you want Python to be able to call the Go method anyway.

The experimental scene

Do a function: TWO-DIMENSIONAL code recognition

There are actually libraries in Python that can do QR code recognition. However, Python3 is not supported if the dependency is severe. In addition, I found that Go has an open source QR code recognition project. Here let Python call the TWO-DIMENSIONAL code recognition interface in Go to complete the experiment.

Go TWO-DIMENSIONAL code identification library address: github.com/tuotoo/qrco…

Now download the project back and delete unnecessary files. Put in a test qr code image test.png

version

  • Python 3.6.4 :: Anaconda custom (x86_64)
  • Go version go1.9.6 Darwin/amd64

The directory structure

├ ─ ─ LICENSE ├ ─ ─ the README. Md ├ ─ ─ example │ ├ ─ ─ CPU - profile. Initial │ ├ ─ ─ groups │ ├ ─ ─ main. Go │ └ ─ ─ test. The PNG ├ ─ ─ qrcode. Go └ ─ ─  version.goCopy the code

Writing Go files

Edit example/main.go as follows:

package main

import (
	"C"
	"fmt"
	"os"
	qrcode "qrcode-master"
)

//export qrcodeText
func qrcodeText(a) *C.char {
	fi, err := os.Open("./test.png")
	if! check(err) {return C.CString("")}defer fi.Close()
	qrcode.Debug = false
	qrmatrix, err := qrcode.Decode(fi)
	check(err)
	retq := qrmatrix.Content
	return C.CString(retq)
}

func check(err error) bool {
	iferr ! =nil {
		fmt.Println(err)
	}
	return err == nil
}

func main(a) {}
Copy the code

Note that the comment //export qrcodeText must be added to the method qrcodeText, which should be used to generate the header file. Otherwise, the *.h file will not be generated when the dynamic link library is compiled later

Build dynamic link libraries

go build -buildmode=c-shared -o rqcode.so main.go
Copy the code

The above command will generate two files rqcode.so and rqcode.h in the current directory.

The file directories are as follows:

├ ─ ─ example │ ├ ─ ─ CPU - profile. Initial │ ├ ─ ─ groups │ ├ ─ ─ main. Go │ ├ ─ ─ qrcode. Py │ ├ ─ ─ rqcode. H │ ├ ─ ─ rqcode. So │ └ ─ ─ ├── ├.go ├─ ├.goCopy the code

Creating a Python file

Create a qrcode.py file in the same directory with the following contents:

from ctypes import cdll, c_char_p

Load the dynamic link library
lib = cdll.LoadLibrary('rqcode.so')

Configure input and output parameter variable types
lib.qrcodeText.argtypes = None
lib.qrcodeText.restype = c_char_p

# call method
rest = lib.qrcodeText()

print(rest.decode('utf-8'))
Copy the code

To begin testing

python qrcode.py
Copy the code

Print out the content of two-dimensional code:

https://github.com/tuotoo/qrcode
Copy the code

During the test, it was found that some two-dimensional codes were unrecognizable, so the Go identification library was incomplete.

conclusion

Based on the above tests, Python and Go actually work well together. We can quickly implement our ideas in Python, and rewrite this with Go when we really hit a performance bottleneck. Of course, this is how Python+C/C++ was developed in the old days, but with C/C++ replaced by Go. But Go is way better than C/C++, don’t you think?