如何设置(或存储)使用GOPATH进行项目

I am not clear of the relationship between GO project with GOPATH. although i have refer so many cases.

I have found there are two solutions

  1. reset(or add something new) the GOPATH when there is a new project need to be add D:\workspace\golang\calcproj;D:\workspace\golang\golearning;D:\workspace\golangpcclient;D:\workspace\golangpcserver

enter image description here

  1. just set a only one GOPATH forever, and just modified the code about import D:\workspace\golang

enter image description here

and when i try to follow the solution 2 to setup the project in Window OS, i always get a error info:can't load package: package calc:

my GOPATH : E:\GoWorks , and Go Project file directory is like below:

enter image description here

i don't like the solution 1,because we need to change the GOPATH every time, when there is a new project~~

Q1. but how to build a project as like the other solution?

Q2. anyway how does GOPATH works ?

Q3. what is the best way to setup the GO project across with GOPATH

How does GOPATH work? GOPATH is where go will look for local dependencies to build your application. This is like the root namespace of your workspace where you are coding. If you change your code location, you likely will want to change your GOPATH, too.

What is the best way to setup the GO project across with GOPATH? This wiki article has your answer

I'd recommend going there and following breadcrumbs if it's moved since I've answered this. However, in case it's not available as you're reading, this is a snippet

Your workspace can be located wherever you like, but we'll use C:\work in this example. Note that GOPATH must not be the same path as your Go installation.

  • Create folder at C:\work.
  • Right click on "Start" and click on "Control Panel". Select "System and Security", then click on "System".
  • From the menu on the left, select the "Advanced systems settings".
  • Click the "Environment Variables" button at the bottom.
  • Click "New" from the "User variables" section.
  • Type GOPATH into the "Variable name" field.
  • Type C:\work into the "Variable value" field.
  • Click OK.

I think for your case, you are adding C:\workspace\golang\...\projectname1;C:\workspace\golang\...\projectname2;... instead of just using C:\workspace as your GOPATH. Keep in mind that this assumes you are using a common namespace for all of your projects, so you'll need to include the \golang\... part of the path in your import section. If you want to exclude the \golang\ part of your package paths, then include that in your GOPATH.