Go-配置Go开发环境

Linux

下载

Go下载 - Go语言中文网 - Golang中文社区

Downloads - The Go Programming Language

version=1.20.2
# 官网下载
wget https://go.dev/dl/go${version}.linux-amd64.tar.gz
# 国内下载
wget https://studygolang.com/dl/golang/go${version}.linux-arm64.tar.gz

配置编译环境

#!/usr/bin/env bash
# set -x

sudo mkdir -p /opt/$USER
sudo chown $USER:$USER /opt/$USER

cd /opt/$USER
version=1.20.2
if [ ! -f go${version}.linux-amd64.tar.gz ]; then
    wget https://studygolang.com/dl/golang/go${version}.linux-amd64.tar.gz
fi
rm -rf /opt/$USER/go${version} && mkdir -p /opt/$USER/go${version}
tar zxf go${version}.linux-amd64.tar.gz -C /opt/$USER/go${version}/ --strip-components=1
rm -f go && ln -sf go${version} go

# export PATH="/opt/$USER/go/bin:$PATH"
. ~/.bashrc
env | grep PATH | grep 'go/bin'
if [ $? -ne 0 ]; then
    echo "export PATH=\"/opt/$USER/go/bin:\$PATH\"" >> ~/.bashrc
fi

# check
which go
go version

MacOS

初始安装

brew search go
brew install [email protected]

其它版本

  • 1.20.2
go install golang.org/dl/go1.20.2@latest
go1.20.2 download
go1.20.2 version
go1.20.2 env GOROOT

export PATH="$(go1.20.2 env GOROOT)/bin:$HOME/go/bin:$PATH"
go version
  • 1.17.8
go get golang.org/dl/go1.17.8
go1.17.8 download
go1.17.8 version
go1.17.8 env GOROOT
export PATH="$(go1.17.8 env GOROOT)/bin:$HOME/go/bin:$PATH"
go version

注:编辑shell配置如~/.zshrc,调整PATH
export PATH="$HOME/sdk/go1.17.8/bin:$HOME/go/bin:$PATH"

Q&A

go多版本切换

  1. gvm
  2. multi GOROOT
  3. brew switch

gvm主要是在LINUX/UNIX系统上管理Golang套件版本的.
Mac OSX最好使用的homebrew
[Golang]透过brew来做Golang的版本切换

参考资料

Download and install - The Go Programming Language

Managing Go installations - The Go Programming Language

batch install on Linux

- name: Deploy go package
  hosts: all
  vars:
    download_url: https://studygolang.com/dl/golang
    deploy_dir: /opt/go
    go_version: 1.20.5
    go_deploy_dir: '{{deploy_dir}}/go{{go_version}}'
    go_home: '{{deploy_dir}}/go'
    go_pkg_amd64: go{{go_version}}.linux-amd64.tar.gz
    go_pkg_arm64: go{{go_version}}.linux-arm64.tar.gz
  tasks:
  - name: create go deploy directory
    file:
      path: '{{go_deploy_dir}}'
      state: directory
      recurse: yes
      owner: '{{ansible_user}}'
    become: true

  - name: download {{go_pkg_arm64}}
    get_url:
      url: '{{download_url}}/{{go_pkg_arm64}}'
      dest: '{{deploy_dir}}/'
      force: no
    connection: local
    run_once: yes
    when: ansible_architecture == 'aarch64'
  - name: download {{go_pkg_amd64}}
    get_url:
      url: '{{download_url}}/{{go_pkg_amd64}}'
      dest: '{{deploy_dir}}/'
      force: no
    connection: local
    run_once: yes
    when: ansible_architecture == 'x86_64'

  - name: Unarchive {{go_pkg_arm64}}
    unarchive:
      src: '{{deploy_dir}}/{{go_pkg_arm64}}'
      dest: '{{go_deploy_dir}}'
      extra_opts: [--strip-components=1]
    when: ansible_architecture == 'aarch64'
  - name: Unarchive {{go_pkg_amd64}}
    unarchive:
      src: '{{deploy_dir}}/{{go_pkg_amd64}}'
      dest: '{{go_deploy_dir}}'
      extra_opts: [--strip-components=1]
    when: ansible_architecture == 'x86_64'
  - name: create soft link
    file:
      src: '{{go_deploy_dir}}'
      dest: '{{go_home}}'
      state: link
  
  - name: set go path env
    lineinfile:
      path: '{{ansible_env.HOME}}/.bashrc'
      insertafter: EOF
      line: '{{item}}'
    with_items:
    - '# Go ENV'
    - 'export PATH={{go_home}}/bin:$PATH'
  - name: check go version
    shell: | 
      export PATH={{go_home}}/bin:$PATH
      which go
      go version
    register: go_info
    run_once: true
  - name: show go info
    debug:
      msg: '{{go_info.stdout_lines}}'

你可能感兴趣的:(Go-配置Go开发环境)