在Go中获取PHP配置变量

I want to get the output of the command php -r 'echo get_cfg_var("some_var");' to check it against a predefined value. Currently, I have the following code:

variableName := "default_mimetype"
cmd := exec.Command("php", "-r", "'echo get_cfg_var(\"" + variableName + "\");'")
out, err := cmd.CombinedOutput()

after running,

err.Error() returns "exit status 254"

out is "PHP Parse error: syntax error, unexpected end of file in Command line code on line 1"

What is causing this error? Am I not escaping something properly?

The problem is your argument. If you change what you have written into a shell command, it would look like the following:

$ php -r "'echo get_cfg_var(\"default_mimetype\");'"

You will notice that there is an extra set of quotes around the 2nd argument that is causing the syntax error. You can fix this by changing your exec.Command to the following:

cmd := exec.Command("php", "-r", "echo get_cfg_var(\"" + variableName + "\");")