How to run go lang scripts using php exec?

0 like 0 dislike
58 views
When run 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)


perform below

export GOPATH=/usr/lib/go

The script in the console fulfills successfully.
Carry out from under PHP:

exec('go run /var/www/mysite.EN/blog/go/main.go 2>&1', $output); print_r($output);


Answer:
Array
(
[0] => sh: 1: go: not found
)

Tried to put the path GOPATH, GOROOT in ~/.bashrc but to no avail. What am I doing wrong?
by | 58 views

3 Answers

0 like 0 dislike
go run main.go


This option run at the design, the finished application is compiled and runs as a binary, and there is no GOPATH and GOROOT not needed.

Compilation is done by command
go build main.go

Read more about GOPATH and GOROOT can read on Habre
\rhttps://habr.com/post/249545/
by
0 like 0 dislike
What am I doing wrong?

Everything. Let's start with the fact that there is no such thing as a "go-script". There are ishodnyj code and Go compiler+linker. The go command build <the name of the file.go> starts the process of compilation and linking. At the exit you get a binary file for the current OS and CPU architecture (default, can be changed through the settings). Then place the resulting binary file to a directory PATH. All. Can run at least via PHP, even through the bash.
by
0 like 0 dislike
GOPATH and GOROOT are needed to work the Go, and you have PHP can't find executable file Go, so either specify the full path (like /usr/local/go/bin/go), or add it to the PATH environment variable the path to it.
by

Related questions

0 like 0 dislike
1 answer
asked May 2, 2019 by TrueDevs
0 like 0 dislike
2 answers
0 like 0 dislike
4 answers
asked May 21, 2019 by tioffs
0 like 0 dislike
2 answers
asked May 20, 2019 by romaaa32
0 like 0 dislike
1 answer
asked May 1, 2019 by Indermove
110,608 questions
257,186 answers
0 comments
33,729 users