I use Neovim to develop my Go project on macOS, but my project only run on Linux Machines, so I need to use to some Linux specific packages like https://pkg.go.dev/golang.org/x/sys@v0.10.0/unix.

Here the issue is: I can’t jump to the definition of xxx_linux.go files, because the GOOS environment variable is set to “darwin” on macOS.

vim.g.GOOS = "linux" can set the GOOS in neovim, but it not works for me, since the GoTest function of vim-go will not function properly.

So I need to set GOOS=linux only for gopls, and keep others as “darwin” by default.

Here is my workaround, set the environment variable before starting gopls.

cd path-to-gopls
mv gopls gopls.bin

cat > gopls.sh <<EOF
#! /bin/bash
# force set goos=linux on macos
# usage: mv gopls gopls.bin; ln -s gopls.sh gopls
export GOOS=linux
bin=\$(realpath "\$0")
exec \$(dirname "\$bin")/gopls.bin "\$@"

chmod +x gopls.sh
ln -s gopls.sh gopls

Now gopls will get GOOS=linux