Go课堂笔记①——Go安装和环境配置

Go课堂笔记

Go安装和环境配置


预习URL

[[讲师博客]: https://www.liwenzhou.com/posts/Go/go_menu/]


课上笔记

markdown格式的笔记+截图


视频

[[链接:]: https://www.bilibili.com/video/BV16E411H7og?p=2]


Go语言开发环境搭建

安装Go开发包

[[国内安装地址]: [https://golang.google.cn/dl/]]


Centos下:

[[linux安装链接]: [https://golang.google.cn/doc/install?download=go1.15.linux-amd64.tar.gz]]

按照该链接出现GOPATH配置错误

[[使用yum安装]: https://www.jianshu.com/p/b2222fc04f47

[[使用yum安装前可以更新为阿里云]: https://www.cnblogs.com/nhdlb/p/11569191.html

注意yum更新后可能会遇到 “No package golang available”,请参考如下方法:

先安装epel:yum install epel-release
然后再yum安装golang

进行环境配置

vi /etc/profile

添加环境变量

# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

# GOROOT
export GOROOT=/usr/lib/golang
# GOPATH
export GOPATH=/root/Work/programmer/go/gopath/
# GOPATH bin
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin


unset i
unset -f pathmunge

在终端执行如下命令立即生效:

source /etc/profile

查看是否安装成功:

go version
Ubuntu下:

按照博客内步骤安装没有遇到问题。

注意:

配置环境变量: Linux下有两个文件可以配置环境变量,其中/etc/profile是对所有用户生效的;$HOME/.profile是对当前用户生效的,根据自己的情况自行选择一个文件打开,添加如下两行代码,保存退出。

修改/etc/profile

source /etc/profile

修改$HOME/.profile

source $HOME/.profile
Windows下:

版本:go1.15.windows-amd64

GOROOTGOPATH都是环境变量,其中GOROOT是我们安装go开发包的路径,而从Go 1.8版本开始,Go开发包在安装完成后会为GOPATH设置一个默认目录,参见下表。

平台GOPATH默认值举例
Windows%USERPROFILE%/goC:\Users\用户名\go
Unix$HOME/go/home/用户名/go

进入“高级系统设置”查看“系统变量”,只需要记住默认的GOPATH路径在哪里就可以了,并且默认情况下 GOROOT下的bin目录及GOPATH下的bin目录都已经添加到环境变量中。

Go1.14版本之后,都推荐使用go mod模式来管理依赖环境了,也不再强制我们把代码必须写在GOPATH下面的src目录了,你可以在你电脑的任意位置编写go代码。(网上有些教程适用于1.11版本之前。)

默认GoPROXY配置是:GOPROXY=https://proxy.golang.org,direct,由于国内访问不到https://proxy.golang.org,所以我们需要换一个PROXY,这里推荐使用https://goproxy.iohttps://goproxy.cn

可以执行下面的命令修改GOPROXY:

go env -w GOPROXY=https://goproxy.cn,direct

这样就完成了Go语言学习的基本物料准备和编译环境的安装,下一篇将介绍如何使用VS code/Sublime进行Go的编译。

人生苦短,Let‘s Go!!!

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值