Go how to set multiple gopaths

Time:2021-9-19

Go set multiple gopaths

linux:


GOPATH="/home/www/gopath1:/home/www/gopath2"

windows:


GOPATH=f:/gopath1;f:/gopath2;

be careful:

Go get is installed to the first gopath path by default

When you go build, you will sometimes report that the same type or method does not match. Because multiple gopath paths are caused by the wrong order, you can change the order

Supplement: how to configure multiple projects in golang (use GB to build projects under non gopath paths)

Scenario 1: write each project path to gopath

Wrote a script setgopath.sh


#!/bin/bash
if [[ $GOPATH =~ .*$PWD.* ]]
then
    echo "currnet dir is already in GOPATH"
else
    export GOPATH=$GOPATH:$PWD
    echo "fininsh setting $PWD in GOPATH"
fi

Run source setgopath.sh in the project home directory to write the current project path to gopath

Scenario 2: use GB instead of go to build the project

This GB is a tool instead of go’s build and test, which allows the project to run the build at any path

GitHub address:https://github.com/constabula…

First, install the GB into the SRC of gopath. If gopath has multiple paths, install it into any path

Make sure your gopath is in the path so that you can call the GB command globally

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.