g

repository·master·Indexed 25 days ago

https://github.com/voidint/g

A command-line tool for Linux, macOS, and Windows designed for managing and switching between multiple Go (Golang) environments. It provides capabilities to install, uninstall, and list Go versions, manage environment variables like G_MIRROR and G_HOME, and includes a Model Context Protocol (MCP) server mode for AI agent integration.

Tokens
4.5K
Snippets
13
Records
37
Agent score
79%

What's inside g

  1. Install g on Linux/macOS

    master

    You can install g automatically using a shell script. It is recommended to clear GOROOT, GOBIN, and other Go-related environment variables before installation to avoid conflicts.

    Automated Installation

    Run the following commands in bash or zsh:

    # Install g
    curl -sSL https://raw.githubusercontent.com/voidint/g/master/install.sh | bash
    
    # Optional: Unalias 'g' if it is already used by another command
    cat << 'EOF' >> ~/.bashrc
    if [[ -n $(alias g 2>/dev/null) ]]; then
        unalias g
    fi
    EOF
    
    # Load the environment
    source "$HOME/.g/env"
    curl -sSL https://raw.githubusercontent.com/voidint/g/master/install.sh | bash
  2. Manage Go versions with g

    master

    Use the g CLI to manage multiple Go installations on your system.

    Common Commands

    CommandDescription
    g ls-remote [status]List available Go versions. Use stable to see only stable versions.
    g install <version>Download and install a specific Go version.
    g lsList currently installed Go versions. The active version is marked with *.
    g use <version>Switch to a different installed Go version.
    g uninstall <version>Remove an installed Go version.
    g cleanClear the cached installation packages.
    g versionShow g version information.
    g self updateUpdate g to the latest version.
    g self uninstallCompletely remove g from your system.

    Examples

    List stable versions:

    g ls-remote stable

    Install and use a version:

    g install 1.20.5

    Switch versions:

    g use 1.19.10
    g install 1.20.5
    g use 1.19.10
    g ls-remote stable
  3. Install g via automated scripts

    master

    You can install g using automated scripts for Linux, macOS, or Windows.

    Important: It is recommended to clear the GOROOT, GOBIN, and other Go-related environment variables before installation to avoid conflicts.

    Linux/macOS (bash/zsh)

    Run the following commands to install and configure your shell profile:

    # Install g
    curl -sSL https://raw.githubusercontent.com/voidint/g/master/install.sh | bash
    
    # Configure shell to handle existing 'g' aliases
    cat << 'EOF' >> ~/.bashrc
    if [[ -n $(alias g 2>/dev/null) ]]; then
        unalias g
    fi
    EOF
    
    # Apply environment changes
    source "$HOME/.g/env"

    Windows (PowerShell)

    Run the following command in PowerShell:

    $ iwr https://raw.githubusercontent.com/voidint/g/master/install.ps1 -useb | iex
    curl -sSL https://raw.githubusercontent.com/voidint/g/master/install.sh | bash
  4. Manual Installation for Linux/macOS

    master

    If you prefer manual installation, follow these steps:

    1. Create a home directory for g (recommended: ~/.g).
    2. Download the binary from the releases page and extract it into ~/.g/bin.
    3. Create a ~/.g/env file to manage environment variables:
    cat >~/.g/env <<'EOF'
    #!/bin/sh
    # g shell setup
    export GOROOT="${HOME}/.g/go"
    [ -z "$GOPATH" ] && export GOPATH="${HOME}/go"
    export PATH="${HOME}/.g/bin:${GOROOT}/bin:${GOPATH}/bin:$PATH"
    export G_MIRROR=https://golang.google.cn/dl/
    EOF
    1. Add the following snippet to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc) to ensure g is initialized on startup:
    if [[ -n $(alias g 2>/dev/null) ]]; then
        unalias g
    fi
    [ -s "${HOME}/.g/env" ] && \. "${HOME}/.g/env"  # g shell setup
    1. Apply the changes: source ~/.bashrc (or your respective config file).
    cat >~/.g/env <<'EOF'
    #!/bin/sh
    # g shell setup
    export GOROOT="${HOME}/.g/go"
    [ -z "$GOPATH" ] && export GOPATH="${HOME}/go"
    export PATH="${HOME}/.g/bin:${GOROOT}/bin:${GOPATH}/bin:$PATH"
    export G_MIRROR=https://golang.google.cn/dl/
    EOF
  5. Update or uninstall g

    master

    The g tool can manage its own lifecycle (requires version >= 1.5.0).

    • Update g to the latest version: g self update
    • Uninstall g from your system: g self uninstall
    • Check current g version: g version
    g self update
  6. Install g on Windows (PowerShell)

    master

    Use PowerShell to install g automatically:

    $ iwr https://raw.githubusercontent.com/voidint/g/master/install.ps1 -useb | iex
    $ iwr https://raw.githubusercontent.com/voidint/g/master/install.ps1 -useb | iex
  7. Manual Installation for Windows (PowerShell)

    master

    To manually install g on Windows:

    1. Create the directory: mkdir ~/.g/bin.
    2. Download the Windows binary from releases and extract it into ~/.g/bin.
    3. (Optional) If g is already an alias for git, rename g.exe to something else, like gvm.exe.
    4. Open your PowerShell profile in VS Code by running code $PROFILE.
    5. Add the following lines to your profile:
    $env:GOROOT="$HOME\.g\go"
    $env:Path=-join("$HOME\.g\bin;", "$env:GOROOT\bin;", "$env:Path")
    1. Restart your PowerShell terminal.
    $env:GOROOT="$HOME\.g\go"
    $env:Path=-join("$HOME\.g\bin;", "$env:GOROOT\bin;", "$env:Path")
  8. Install g manually on Linux/macOS

    master

    If automated installation is not suitable, follow these steps for Linux/macOS:

    1. Create a directory for g (recommended: ~/.g).
    2. Download the binary compressed file from releases and unzip it into the bin subdirectory (e.g., ~/.g/bin).
    3. Create a ~/.g/env file to manage environment variables:
    cat >~/.g/env <<'EOF'
    #!/bin/sh
    # g shell setup
    export GOROOT="${HOME}/.g/go"
    [ -z "$GOPATH" ] && export GOPATH="${HOME}/go"
    export PATH="${HOME}/.g/bin:${GOROOT}/bin:${GOPATH}/bin:$PATH"
    export G_MIRROR=https://golang.google.cn/dl/
    EOF
    1. Import the environment into your shell configuration (e.g., ~/.bashrc or ~/.zshrc):
    cat >>~/.bashrc <<'EOF'
    if [[ -n $(alias g 2>/dev/null) ]]; then
        unalias g
    fi
    [ -s "${HOME}/.g/env" ] && \. "${HOME}/.g/env"  # g shell setup
    EOF
    1. Enable the variables by sourcing the config: source ~/.bashrc.
    cat >~/.g/env <<'EOF'
    #!/bin/sh
    # g shell setup
    export GOROOT="${HOME}/.g/go"
    [ -z "$GOPATH" ] && export GOPATH="${HOME}/go"
    export PATH="${HOME}/.g/bin:${GOROOT}/bin:${GOPATH}/bin:$PATH"
    export G_MIRROR=https://golang.google.cn/dl/
    EOF
  9. Install g manually on Windows PowerShell

    master

    To install g manually on Windows:

    1. Create the directory: mkdir ~/.g/bin.
    2. Download the Windows binary from releases, unzip it, and place it in ~/.g/bin.
    3. (Optional) If you use g as an alias for Git, rename g.exe to something else like gvm.exe.
    4. Open your PowerShell profile in VSCode by running code $PROFILE.
    5. Add the following configuration to your profile:
    $env:GOROOT="$HOME\.g\go"
    $env:Path=-join("$HOME\.g\bin;", "$env:GOROOT\bin;", "$env:Path")
    1. Restart PowerShell to use the g or gvm command.
    mkdir ~/.g/bin
  10. Configure g experimental features

    master

    Use the G_EXPERIMENTAL environment variable to enable experimental features in g.

    • Enable all experimental features: Set G_EXPERIMENTAL=true.
    • Custom Home Directory: Once G_EXPERIMENTAL=true is set, you can use the G_HOME environment variable to change the default ~/.g directory to a custom path.
  11. Configure G_MIRROR for Go downloads

    master

    The G_MIRROR environment variable allows you to specify mirror sites for querying and downloading Go versions. This is useful in regions where the official Go website is difficult to access.

    Supported Collector Types

    You can specify a collector type using the format collector_name|URL:

    • Official Collector: Uses the official Go download page structure. Example: G_MIRROR=official|https://golang.google.cn/dl/
    • FancyIndex Collector: For Nginx FancyIndex modules. Example: G_MIRROR=fancyindex|https://mirrors.aliyun.com/golang/
    • AutoIndex Collector: For Nginx AutoIndex modules. Example: G_MIRROR=autoindex|https://mirrors.ustc.edu.cn/golang/

    Known Mirror Sites

    • https://golang.google.cn/dl/
    • https://mirrors.aliyun.com/golang/
    • https://mirrors.nju.edu.cn/golang/
    • https://mirrors.hust.edu.cn/golang/
    • https://mirrors.ustc.edu.cn/golang/
  12. Configure g environment variables

    master

    Use the following environment variables to customize g behavior:

    VariableDescription
    G_MIRRORSpecifies one or multiple mirror sites for downloading Go versions (comma-separated). Supports collectors like official|, fancyindex|, or autoindex|.
    G_EXPERIMENTALSet to true to enable experimental features, such as custom G_HOME support.
    G_HOMESets the home directory for g (defaults to ~/.g). Requires G_EXPERIMENTAL=true.
    HTTP_PROXY / HTTPS_PROXYStandard proxy variables used by g for network requests.

    Example G_MIRROR usage with collectors:

    • G_MIRROR=official|https://golang.google.cn/dl/
    • G_MIRROR=fancyindex|https://mirrors.aliyun.com/golang/