A
A
Alex Smith2018-04-27 13:44:23
PHP
Alex Smith, 2018-04-27 13:44:23

How to run go lang scripts with php exec?

When executed from the console:
go run main.go
Outputs:

cannot find package "github.com/PuerkitoBio/goquery" in any of:
        /usr/lib/go/src/github.com/PuerkitoBio/goquery (from $GOROOT)

I execute below
export GOPATH=/usr/lib/go
The script in the console fulfills successfully.
I execute from under PHP:
exec('go run /var/www/mysite.ru/blog/go/main.go 2>&1', $output);
print_r($output);

Answer:
Array
(
[0] => sh: 1: go: not found
)
I tried to write GOPATH, GOROOT paths in ~/.bashrc but didn't help. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Papa, 2018-04-27
Stifflera @PapaStifflera

What am I doing wrong?

Everything. Let's start with the fact that there is no such thing as a "go-script". There is a source code in Go and a compiler + linker. The go build <filename.go> command starts the compilation and linking process. At the output, you will get a binary file for the current operating system and processor architecture (by default, you can change through the parameters). Next, place the resulting binary file in a directory from PATH. All. You can run through PHP, even through bash.

V
Vitaly Arkhipov, 2018-04-27
@arvitaly

GOPATH and GOROOT are needed for Go itself to work, and your PHP can't find the Go executable, so either specify the full path to it (like /usr/local/go/bin/go) or add the path to the PATH environment variable to him.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question