phper all know that php-extension is written in C/C.
We use C/C due to development efficiency issues Golang writes php-extension
Make full use of golang's development efficiency and ecology to contribute to PHP.
Environment preparation
wsl ubuntu golang 1.13 php 7.3
New project
php7.3 里面的 ext_skel 和以前版本参数不一样,请查看 help 操作
php: ./ext_skel.php --ext goserver --onlyunix golang: mkdir goserver
Encapsulation file
Encapsulation C file into golang file
package main /* php扩展.C文件内容 */ import "C" func main() {}
build
$ phpize $ ./configure $ CGO_CFLAGS="-g -O2 -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 \ -I. -DPHP_ATOM_INC \ -I/usr/include/php/20180731/include \ -I/usr/include/php/20180731/main \ -I/usr/include/php/20180731 \ -I/usr/include/php/20180731/main \ -I/usr/include/php/20180731/TSRM \ -I/usr/include/php/20180731/Zend \ -I/usr/include/php/20180731/ext \ -I/usr/include/php/20180731/ext/date/lib \ -DHAVE_CONFIG_H -DCOMPILE_DL_GOPHP -fPIC -DPIC" \ CGO_LDFLAGS="-Wl,--export-dynamic -Wl,--unresolved-symbols=ignore-all -Wl,-z,nodelete" \ go build -p 1 -gcflags "-l" -buildmode=c-shared -o modules/goserver.so goserver.go
CGO compilation parameters Fill in according to the actual situation, you can check the parameters in the generated makefile file
run
Modify php.ini and add extension=goserver.so
Run the extension that comes with it Function test