How to set golang gopath

(*-*)浩
Release: 2019-12-03 10:28:10
Original
8737 people have browsed it

How to set golang gopath

The requirement to set GOPATH is a major issue for Go users who install Go tools for the first time and get the error: "you have to set a Republican Still need to add $GOPATH/bin to their PATH to run the executable initialized by go install to execute go get

Users developing in Go language still need to understand the existence, location and structure of GOPATH. If your GOROOT (where you check Go's source code) is the default GOPATH, the tool will refuse to break your GOROOT using the default GOPATH if no GOPATH is set. If the default doesn't suit you, you may still prefer to set a custom GOPATH.

GOPATH definition

GOPATH environment variable is used to specify the location of your workspace. If GOPATH is not set, it will be automatically set in a Unix-like system like this :

$HOME/go
Copy after login

Set in windows system like this:

%USERPROFILE%\go
Copy after login
If you want to customize your workspace, you need to set the GOPATH environment variable. Here’s how How to set it up on different platforms.

Customize GOPATH on different platforms

Unix system

GOPATH can be set in any directory of your system Next. In the following example we will set it to $HOME/go (default after Go 1.8 version). Note that GOPATH cannot be the same as your go installation directory. The common setting is GOPATH=$HOME.

Bash

Edit ~/.bash_profile and add the following line:

export GOPATH=$HOME/go
Copy after login
Copy after login

Save and exit the editor, then execute

source ~/.bash_profile
Copy after login

Zsh

Edit your ~/.zshrc and add the following line:

export GOPATH=$HOME/go
Copy after login
Copy after login

Save and exit your editor. Then execute

source ~/.zshrc
Copy after login

fish

set -x -U GOPATH $HOME/go
Copy after login

-x is used to specify the variables you want to export -U is set to the global environment

Windows system

Your workspace can be defined into any directory you want to place it, below we use C:\go-work as an example

Note: GOPATH cannot be the same as the Go installation directory.

Windows 7

Create directory C:\go-work.

Right-click on the computer icon and select Properties

Select Advanced System Settings on the left Select the bottom Environment variable

Select GOPATH in the upper window Click to edit the input value C:\go-work Save

Windows 10

There is a faster one Way by searching for environment variables:

Left-click "Search" and type env or environment.

Select "Edit environment variables for your account". And follow the above windows7 steps.

View GOPATH

go env GOPATH
Copy after login

The above is the detailed content of How to set golang gopath. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!