Problems with GOPATH environment variable

Golang version 1.8.3
macOS 10.12.5 Sierra
Fish shell

Hello, I have a setup where I am using Fish as an alternative shell to Bash, and for purely Golang activities which operate within the shell, this works fine. However, I am also trying to build a GUI app using Lazarus. Amongst other things, this app attempts to read the GOPATH using a function called (not unreasonably) GetEnvironmentVariable. The problem I have is that in a GUI application this doesn’t work - the function just returns a #0 terminated string. The weird thing is that a console application, written in Lazarus, using the same GetEnvironmentVariable function returns the correct value for GOPATH.

Somebody on a different forum has suggested a likely sounding cause of this problem, but unfortunately, the suggested fix didn’t seem to work.

https://forum.lazarus.freepascal.org/index.php/topic,37239.15.html

It seems to me that the reason the GOPATH environment variable is not fully visible is that I am only setting it in the Fish.config file rather than somewhere more fundamental. I’ve attempted to set me system up so that it starts in Bash, sets the environment variables, and then executes Fish, but I haven’t managed to get this working yet.

The Lazarus app, incidentally, is a Golang file preview utility with syntax highlighted text colouring. If I can get this problem solved it will, of course, be released as open source.

I am not sure if I fully understand your problem (as there are several layers involved - an IDE, a GUI app, a “GetEnvironmentVariable” function of unknown origin), but in general, if you want a macOS GUI app pick up environment variables, you have to either set them in /etc/launch.conf or (for the current session) via launchctl setenv.
This stackexchange post summarizes three approaches (also visit the three links at the top of the post for details on each approach).

1 Like

Hi Christoph, thanks for the reply. Your suggestion almost agrees with the one I linked to. The only difference is that you mention /etc/launch.conf whereas the other suggestion talked about /etc/launchd.conf I’ve just looked at the StackExchange post, I assume that launch.conf is a typo.

You’re right, it is a typo, sorry for that.

The post in the Lazarus forum only talks about the conf file. Did you try the launchctl setenv approach as well?

And another option just came to my mind, maybe even better than fighting launch d(a)emons. Your app requires the GOPATH to be set, and this means that Go has to be installed. So the app can simply ask the go command for the GOPATH by calling

go env GOPATH

and capturing the output.

Advantages:

  • The target machine does not need to have a system-wide GOPATH set.
  • This also works if $GOPATH is not set at all. (In this case, the GOPATH defaults to $HOME/go since Go 1.8.)
  • And this works across operating systems.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.