Home  >  Article  >  Backend Development  >  How to run Golang on different operating systems

How to run Golang on different operating systems

PHPz
PHPzOriginal
2023-04-14 09:33:18721browse

Golang (also known as Go) is an open source programming language developed by Google. Released in 2009, the language draws inspiration from C and C, but it is also influenced by many other programming languages. Golang is a statically typed programming language that aims to be efficient, concise, and reliable, while its syntax is designed to be easy to use and understand.

In this article, we will introduce how to run Golang on different operating systems.

Running Golang on Windows

Official website installation

First of all, to install Golang on Windows, the easiest and most recommended way is to download and install it from the official website. The download link is https://golang.org/dl/.

After the download is complete, double-click to open the installer and follow the instructions of the installation wizard. The installer will set the necessary system environment variables for you.

PowerShell Installation

You can also install Golang using PowerShell, a command-line shell on Windows. You can perform the following steps:

  1. Open PowerShell;
  2. Determine your architecture (32-bit or 64-bit) to determine the version you need to install;
  3. Run the following command to install:
Invoke-WebRequest -Uri "https://dl.google.com/go/go1.16.4.windows-amd64.msi" -OutFile go1.16.4.windows-amd64.msi
msiexec.exe /i go1.16.4.windows-amd64.msi /quiet

Environment variable configuration

Whether you download and install from the official website or install using PowerShell, you need to add the Golang bin directory to the system environment variables . Please follow the steps below:

  1. Open "Run" through the shortcut key Win R;
  2. Enter sysdm.cpl, and then press the Enter key to open the system properties Window;
  3. Click the "Advanced" tab, and then click the "Environment Variables" button below;
  4. Find the "System Variables" area, view the PATH variable, and changeC:\ Go\bin to your PATH (if you are using PowerShell, the path may be slightly different).

Now you have Golang installed and configured on Windows!

Running Golang on macOS

Homebrew installation

You can use Homebrew to manage your Golang installation. Homebrew is a package manager on macOS. If Homebrew is not installed, open a terminal and execute the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Once Homebrew is installed, install Go by running the following command:

brew install go

Now you are installed on macOS Golang!

Running Golang on Linux

Installation Package Manager

If you are using one of the supported Linux distributions such as Ubuntu, Debian, Fedora or CentOS, You can install Golang using the corresponding package manager.

Ubuntu and Debian

To install Golang on Ubuntu and Debian, use the following command:

sudo apt update
sudo apt install golang

Fedora

To install Golang on Fedora, Please use the following command:

sudo dnf install golang

CentOS

To install Golang on CentOS, please use the following command:

sudo yum install golang

Manual installation

If your Linux If the distribution does not support Golang or you want to install another version, you can install it manually.

  1. Go to the official website (https://golang.org/dl/) to download the version suitable for your operating system and architecture;
  2. Create directory~/go;
  3. Unzip the file and place it in the ~/go directory;
  4. in .bashrc (if you are using bash) Or add the following lines to .zshrc (if you are using zsh):
export GOROOT=~/go
export GOPATH=$HOME/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
  1. Override the new environment variable:
source ~/.bashrc # Or your corresponding shell configuration file

Now, you have installed Golang on Linux!

Run your first Golang program

No matter which operating system you are using, after installing Golang, you can create your first Golang program.

  1. Create a new file named hello.go;
  2. Add the following code:
package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
  1. Save and exit;
  2. In a terminal, execute the program using the following command:
go run hello.go

You should see the output Hello, world!.

Summary

Golang is a very popular programming language that is favored due to its efficiency, simplicity and reliability. In this article, we cover how to install and configure Golang on Windows, macOS, and Linux, as well as how to write and run your first Golang program.

The above is the detailed content of How to run Golang on different operating systems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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