golang 安装指南

Author

Reads 552

Man Working on Computers Coding
Credit: pexels.com, Man Working on Computers Coding

Go的安装包可以从官方网站下载。我们可以选择适合自己的操作系统。

Installation Methods

Go has multiple installation methods, and you can choose the one that suits you best. You can install Go from source, use the standard package installer, or use third-party tools like apt-get and homebrew.

If you choose to install Go from source, you'll need to download the source code and compile it. This method is suitable for developers who want to customize their installation. For example, on Ubuntu, you can install the necessary tools with `sudo apt-get install gcc libc6-dev`.

Alternatively, you can use the standard package installer, which is a convenient way to install Go on Windows, Linux, and Mac. This method is recommended for those who want a quick and easy installation. You can download the package from the official Go website and follow the installation instructions.

Here are some common installation methods for Go:

  • Go from source: This method requires downloading and compiling the source code.
  • Go standard package installer: This method provides a convenient way to install Go on various platforms.
  • Third-party tools: Tools like apt-get and homebrew can be used to install Go on specific platforms.

Three Methods

There are three main methods to install Go, and I'll break them down for you.

Man Working on Computers Coding
Credit: pexels.com, Man Working on Computers Coding

Method 1: Installing from Source

You can install Go from source, which means you'll be downloading the Go source code and compiling it yourself. This method is recommended for developers who want to customize their Go installation.

Method 2: Using a Package Manager

Alternatively, you can use a package manager like apt-get or homebrew to install Go. This method is convenient and easy to use, especially for beginners.

Method 3: Using a Third-Party Tool

Another option is to use a third-party tool like GVM to manage multiple versions of Go on your system. This method is useful for developers who need to work with different versions of Go.

Here are the three installation methods in a nutshell:

Each method has its own advantages and disadvantages, and the right choice for you will depend on your specific needs and preferences.

On a similar theme: GSAT-16

Proxy

Proxy settings can be configured to control access to the module mirror.

The value of GOPROXY in $GOROOT/go.env is set to direct, which disables access to the module mirror.

A project-specific go.env can override this setting.

You can also set a user-specific override with GOPROXY in your go.mod file.

For more information on GOPROXY and the module mirror, check out the official documentation at https://proxy.golang.org.

You might enjoy: Golang Go

Installation Process

Credit: youtube.com, 1.2 Golang安装以及环境配置 | Golang课程 | 编程语言学习

Installing Go on your system is a straightforward process. You can install Go using the package manager on your Linux distribution, such as apt on Debian or Ubuntu.

To install Go using apt, update your package list and then install the Go package. The latest version available through the default source is Go 1.7, but you can install Go 1.10 from the backports source. This is recommended for its improved features.

Once installed, you can verify the installation by running the command `go env`. This will display the Go environment variables, including the GOPATH, which should be set to your user's HOME directory.

Standard Package

The standard package in Go is a one-click installation option that comes with the language. This option is available for each platform and installs the package by default in the directory /usr/local/go (or c:\Go on Windows).

You can change the installation location, but you'll need to set up the corresponding environment variables. For Mac and Unix users, it's best to write these commands in the .bashrc or .zshrc file. For Windows users, you'll need to write them in the environment variables.

The Go standard package will become available on your system after installation.

Sum Db

A young man with glasses drinks soda while working on his computer in a vintage office setting.
Credit: pexels.com, A young man with glasses drinks soda while working on his computer in a vintage office setting.

The Sum DB is a crucial part of the Go installation process.

It's used to help validate downloaded modules, and its value is set by the GOSUMDB environment variable in $GOROOT/go.env.

The default value is sum.golang.org, but it can be overridden by a project-specific go.env.

You can also set a user-specific override with the GOSUMDB environment variable.

The GOSUMDB variable identifies the name of the checksum database used for validation.

System Requirements

To install Go, you'll need a computer with a 64-bit operating system, such as Windows 10 or macOS High Sierra.

The Go compiler requires at least 8 GB of RAM to run smoothly.

You can download the Go installer from the official Go website, which is available in multiple languages, including English, Chinese, and Japanese.

For your interest: Golang vs Go

Standard Suite

The Go standard suite can be installed with just one click on each platform, and it defaults to installing in the /usr/local/go directory on Unix systems, or c:\Go on Windows.

You can change the installation location if you want, but be sure to update your environment variables accordingly.

For Mac and Unix users, it's a good idea to write these commands into your .bashrc or .zshrc file.

Linux

Credit: youtube.com, What Are the System Requirements for Installing the Linux Operating System?

To install Go on Linux, you'll need to download the correct version for your system architecture. Download the 32-bit version for a 32-bit system and the 64-bit version for a 64-bit system.

The file you'll be downloading is called go1.8.3.linux-386.tar.gz for 32-bit systems and go1.8.3.linux-amd64.tar.gz for 64-bit systems.

You can install Go in any directory you like, but you'll need to set the installation directory as an environment variable. You'll also need to set the GOPATH variable, which is where your Go projects will be stored.

To install Go, extract the tar.gz package to your installation directory using the command tar zxvf go1.8.3.linux-amd64.tar.gz -C $GO_INSTALL_DIR.

See what others are reading: Golang Set Env Variable

Installation Tools

To install Go with ease, you can use a third-party tool called GVM, which is similar to rvm for Ruby. GVM is a Go multiple version manager that makes installation a breeze.

GVM can be installed using a simple command, and once installed, you can install Go using another command. You can also use a command like "gvm use go1.8.3 --default" to set the default version and avoid having to call "gvm use" every time.

After running this command, your GOPATH, GOROOT, and other environment variables will be automatically set, making it easy to start coding with Go right away.

For your interest: Os Args Golang

Toolchain

Two business professionals brainstorming and planning software development with a whiteboard in an office.
Credit: pexels.com, Two business professionals brainstorming and planning software development with a whiteboard in an office.

The Go toolchain is a collection of tools that comes bundled with the Go language. It includes the compiler, standard library, assembler, and other tools.

In Go 1.21, a new feature called GOTOOLCHAIN was introduced, which allows for project-specific choices for the Go language toolchain.

The value of GOTOOLCHAIN is set to local by default, but it can be overridden in a project-specific go.env file.

Setting GOTOOLCHAIN to local ensures that the go command always runs the bundled Go toolchain.

You can also set a user-specific override with a command, giving you more control over your project's toolchain settings.

Curious to learn more? Check out: Golang Test Command

Gvm

Gvm is a third-party developed Go multiple version management tool, similar to rvm for Ruby. It's quite convenient to use and can be installed using the command `curl -s https://raw.githubusercontent.com/moovweb/gvm/master/binary/install | bash`.

After installation, you can install Go using the following command. Alternatively, you can use `gvm use go1.8.3 --default` to install and set Go 1.8.3 as the default version, which will automatically set up GOPATH, GOROOT, and other environment variables.

Executing these commands will set up your environment variables, allowing you to use Go directly. To complete the setup, you'll need to reload your profile file.

For another approach, see: Gcloud Api Using Golang

Installation Details

Credit: youtube.com, Window 系统下 Go 的安装和环境配置,GOROOT 和 GOPATH 是关键

The default source installation method for Debian 9 is outdated, with the latest version being Go 1.7. However, you can install Go 1.10 using the backports source.

To install Go using the default source, you'll need to update the package list and then install the Go package. The command to install Go is `sudo apt install golang`. However, this will install an outdated version of Go.

The GOPATH variable is not set by default, which can cause issues with Go's package management. To set the GOPATH variable, you'll need to manually specify it. Go 1.8 and later versions automatically set the GOPATH variable to the user's home directory.

If you're using Debian 9, you can install Go 1.10 using the backports source. To do this, you'll need to add the backports source to your package list and then install the Go package. The command to add the backports source is `sudo sed -i 's/# deb http://deb.debian.org/debian-stable-backports/ deb http://deb.debian.org/debian-backports/ /etc/apt/sources.list.d/backports.list` and then update the package list with `sudo apt update`.

For more insights, see: Simple Http Server Golang Github

Credit: youtube.com, Go从入门到精通,只需要24天 P3 安装Go开发包

The command to install Go using the backports source is `sudo apt install golang-go`. This will install Go 1.10, which is the latest version available in the backports source.

If you're using a different Linux distribution, you can install Go using the package manager. For example, on Ubuntu, the command to install Go is `sudo apt install golang-go`. On Alpine Linux, the command is `sudo apk add go`.

Operating System

For UNIX/Linux/Mac OS X, and FreeBSD systems, you can use the source code installation method. This involves downloading the binary package go1.4.linux-amd64.tar.gz.

You'll need to extract the package to the /usr/local directory. Then, add the /usr/local/go/bin directory to your PATH environment variable. However, this change will only be temporary and will be lost when you close your terminal.

To make the change permanent, you can edit the ~/.bash_profile or /etc/profile file and add the necessary command to the end of the file. After making the change, you'll need to execute the command to apply it.

Here's an interesting read: Golang Add to Map

Unix/Linux/Unix-like OS

Credit: youtube.com, Introduction To Unix and Unix-like operating systems

You can install Go on Unix/Linux/Unix-like OS systems using the source code installation method. This involves downloading a binary package, such as go1.4.linux-amd64.tar.gz.

To install Go, download the binary package and extract it to the /usr/local directory. The extracted files will include the Go binary in the /usr/local/go/bin directory.

To use the Go binary, you need to add the /usr/local/go/bin directory to your PATH environment variable. You can do this temporarily by adding the directory to your terminal's PATH, but this will only last until you close the terminal.

A more permanent solution is to edit your ~/.bash_profile or /etc/profile file and add the following command to the end of the file. This will set the PATH environment variable permanently.

Note that on MAC systems, you can use the .pkg installation package to install Go, which will place the installation directory at /usr/local/go/.

For your interest: Install Golang Package

如何判斷自己的作業系統是32位還是64位?

To determine if your operating system is 32-bit or 64-bit, you need to know the type of your system. Windows users can do this by pressing Win+R, typing "systeminfo" and pressing Enter, then looking for "系統型別" which will show "x64-based PC" for 64-bit or "X86-based PC" for 32-bit.

If you're using a Mac, it's recommended to use a 64-bit system as older Mac OS X versions don't support 32-bit processors.

Linux users can check their system type by running the command "arch" or "uname -m" in the Terminal.

Recommended read: Install Golang in Mac

1.1 Environment

Credit: youtube.com, 【零基础入门学Golang】 第一节 开发环境安装

To install Go, head to the download page at https://golang.google.cn/dl/.

You'll need to add the installation directory, D:\Go\bin, to your system's environment variable Path.

Open PowerShell in the installation directory and run a command to output "Hello, World!", which indicates a successful installation.

Judith Lang

Senior Assigning Editor

Judith Lang is a seasoned Assigning Editor with a passion for curating engaging content for readers. With a keen eye for detail, she has successfully managed a wide range of article categories, from technology and software to education and career development. Judith's expertise lies in assigning and editing articles that cater to the needs of modern professionals, providing them with valuable insights and knowledge to stay ahead in their fields.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.