This is a creation in Article, where the information may have evolved or changed.
Gopher often encounter in a number of $gopath back and forth to switch work scene, each switch has to manually modify $gopath to compile and run the current project, it is inconvenient!! So is there a way to automatically modify $gopath? The answer, of course, is yes!
I implemented the LINUX/MAC command line by configuring the ' ~/.ZSHRC ' file to automatically reset the $gopath function when the window is started and the CD command is called.
The idea is:
First, determine whether the current directory is in a ' src ' directory;
If it is, then further determine whether the current path corresponds to the Gopath has been set to the environment variable;
If not set, the $gopath is automatically modified to the current path.
Step one: Open the ' ~/.ZSHRC ' file
vim ~/.zshrc
Step Two: Copy the following code into the ' ~/.ZSHRC ' file and save
function setgopath() {currpath=`pwd`gopath=${currpath%/src*}if [[ $currpath != $gopath ]];then if [[ $gopath != $GOPATH ]];then export GOPATH=$gopath echo '$GOPATH:'$GOPATH fifi}setgopathfunction cd_and_setgopath() {cd $1setgopath}alias cd='cd_and_setgopath'
Step three: Make the configuration effective
source ~/.zshrc