Can I set multiple $GOPATH?

Hi :slight_smile: I am learning go-lang.

Sometimes, I am uncomfortable because I have to reset $GOPATH manually when I set up some project.
This is really tiresome work to me.

Is there any solution about this problem?

Thank you.
Sorry for my English.

1 Like

You only need to setup $GOPATH once and need not to worry about it. It is just to facilitate go automation like caching module packages and store installed go binary.

As for current project, you should already be using Go module so you can literally works anywhere inside or outisde GOPATH.

2 Likes

Hi @hollowaykeanho :slight_smile:

What I was wondering is maybe not your answer.

Let’s say we will construct two projects and these are different folder each other.
First day, we work in first project so we should set $GOPATH to first project folder.
Second day, we work in second project so we should reset $GOPATH to second project folder.


This is really tiresome work. right?

2 Likes

Hi @rot13

WoW. this is what I really wanted to find it! Thanks @rot13 :slight_smile:

But, the location which will be downloaded some packages is maybe still my work.

1 Like

My point is you don’t need to keep switching GOPATH between projects.

The 2 projects has their own go.mod that defines and manage the project dependencies independently.

When you compile/run the projects, go module will download dependencies repo and cache it in GOPATH/pkg. Similarly, when you go get a binary, it installs stuff inside GOPATH/bin or GOBIN if defined. That’s all about GOPATH caching role.

If you keep switching GOPATH, you bound to have duplicated repo caches and go binaries, which wasted a lot of spaces and bandwidth.

Unless you are really required to do so, you can script a same name BASH script (e.g. config.sh) in each projects defining the environment variables.

#!/bin/bash
export GOPATH=/project/gopath

Then you can source the script each time you switch project.

$ cd /path/to/project
$ source ./config.sh 
1 Like

using go.mod to management ur go projects require packages
and GOPATH just the bin and lib location

then

cd /your/project/path
go install .

or multi build in a project

cd /your/project/path
go install ./build1
go install ./build2

example: my projects workspace as below

1 Like

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