Linux/mac Golang Gopath working path on the command line in a dynamic setting

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.