Go version完全指南:安装、配置与使用 – wiki大全

Go Version Complete Guide: Installation, Configuration, and Usage

Go, also known as Golang, is an open-source programming language developed by Google. It’s renowned for its simplicity, efficiency, and strong support for concurrency, making it a popular choice for building high-performance network services, command-line tools, and more. This comprehensive guide will walk you through the process of installing, configuring, and getting started with Go on various operating systems.

1. Installation

Always download Go from the official Go website to ensure you get the latest stable release and the correct installer for your system architecture.

Windows

  1. Download the Installer: Visit golang.org/dl/ and download the .msi installer for Windows.
  2. Run the Installer: Double-click the downloaded .msi file and follow the on-screen instructions. By default, Go is typically installed to C:\Go.
  3. Verify Environment Variables: The installer usually configures the PATH environment variable automatically. To verify or manually set it:
    • Right-click on “This PC” (or “My Computer”) and select “Properties”.
    • Click “Advanced system settings” and then “Environment Variables”.
    • Under “System variables”, find and edit the Path variable.
    • Ensure that C:\Go\bin (or your custom installation path) is included. Modern Go installations often don’t require GOROOT to be set manually.
  4. Verify Installation: Open a new Command Prompt or PowerShell and type go version. You should see the installed Go version.

macOS

  1. Download the Installer: Go to golang.org/dl/ and download the .pkg installer for macOS.
  2. Install Go: Run the .pkg file and follow the instructions. Go is usually installed to /usr/local/go.
  3. Verify Environment Variables: The installer should automatically add /usr/local/go/bin to your PATH. You might need to restart your Terminal session. If manual configuration is needed, add the following line to your shell profile file (e.g., ~/.bash_profile, ~/.zshrc, or ~/.profile):
    bash
    export PATH=$PATH:/usr/local/go/bin
  4. Verify Installation: Open a new Terminal window and type go version.
  5. Alternative (Homebrew): If you use Homebrew, you can install Go with:
    bash
    brew install go

Linux

  1. Download the Tarball: Navigate to golang.org/dl/ and download the latest .tar.gz archive for Linux.
  2. Extract and Install:
    • Remove any previous Go installation: sudo rm -rf /usr/local/go
    • Extract the archive to /usr/local (replace go1.x.x.linux-amd64.tar.gz with the actual filename):
      bash
      sudo tar -C /usr/local -xzf go1.x.x.linux-amd64.tar.gz
  3. Set Up Environment Variables: Add Go’s binary directory to your PATH. Append this line to your shell profile file (e.g., ~/.bashrc, ~/.zshrc, or ~/.profile):
    bash
    export PATH=$PATH:/usr/local/go/bin

    To apply changes immediately, run source ~/.bashrc (or your respective profile file).
  4. Verify Installation: Open a new Terminal and type go version.
  5. Alternative (Ubuntu/Debian): On Ubuntu-based systems, you can install Go using apt:
    bash
    sudo apt update
    sudo apt install golang-go

2. Go Environment Variables

Go utilizes several environment variables to manage its behavior and workspace.

  • PATH: This essential system variable dictates where your operating system looks for executable programs. During installation, Go’s bin directory (e.g., C:\Go\bin, /usr/local/go/bin) is added to PATH, allowing you to execute go commands from any directory.
  • GOROOT: This variable points to your Go installation directory. For modern Go versions (Go 1.8 and later), the Go tools usually infer this value, making manual configuration often unnecessary.
  • GOPATH: This variable defines the root of your Go workspace, where your Go projects, source code, and downloaded modules are stored.
    • By default, GOPATH is $HOME/go on Unix-like systems and %USERPROFILE%\go on Windows.
    • You can customize GOPATH. For Go 1.13 and newer, you can set it using go env -w GOPATH=$HOME/my_go_workspace or by manually adjusting your environment variables.
    • Crucially, GOPATH should not be the same as your GOROOT (Go installation directory).

3. Basic Usage

With Go installed and configured, you’re ready to start coding!

Create a Go Module

Go modules are the standard way to organize Go projects and manage dependencies.
1. Create a new directory for your project:
bash
mkdir myproject
cd myproject

2. Initialize a Go module within your project:
bash
go mod init example.com/myproject

This command creates a go.mod file, which tracks your project’s dependencies and module path.

“Hello, World!” Program

Let’s write a classic “Hello, World!” program.
1. Create a file named main.go inside your myproject directory.
2. Add the following code to main.go:
“`go
package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}
```

Run the Program

To execute your Go program directly:
1. Open your terminal in the myproject directory.
2. Run the program using go run:
bash
go run main.go

This command compiles and executes your code.

Build an Executable

To compile your code into a standalone executable binary:
1. In your terminal, within the myproject directory, use go build:
bash
go build -o myapp

This creates an executable file named myapp (or myapp.exe on Windows) in the current directory.
2. You can then run the executable directly:
* On Linux/macOS: ./myapp
* On Windows: .\myapp.exe

Conclusion

You’ve successfully installed Go, configured its essential environment variables, and run your first Go program. This guide provides a solid foundation for your Go development journey. For more in-depth learning, explore the official Go documentation at go.dev, which offers extensive tutorials and reference materials. Happy coding!

滚动至顶部