02《Go语言入门》编写第一个Go程序Hello, World

安装Go的步骤,以及编写第一个Go程序”Hello, World!”。

⚡️这是我纯手写的《Go语言入门》,源码+文章,手把手教你入门Go。看了你就会!
⚡️文章中所有的代码我都放到了 github.com/GanZhiXiong/go_learning 这个仓库中!
⚡️看文章时,对照仓库中代码学习效果更佳哦!

环境安装

下载安装包

  1. 进入Golang官网
  1. 下载安装包

安装

macOS安装

如果下载的是pkg文件,则直接双击运行安装;
如果下载的是tar.gz压缩文件,则按照Linux安装步骤进行安装。

Linux安装

  1. 在终端将包解压到/usr/local目录下

    1
    tar -C /usr/local -xzf 压缩包文件名 # 如go1.15.6.linux-amd64.tar.gz
  2. 配置环境变量
    $HOME/.profile/etc/profile中(对于系统范围的安装)添加如下脚本

    1
    export PATH=$PATH:/usr/local/go/bin
  3. 执行source命令

    1
    2
    3
    source $HOME/.profile
    # or
    source /etc/profile

Windows安装

直接下载.msi文件,下载后直接安装。
默认是安装在C:\Go目录下,安装包的形式安装应该是不需要再配置环境变量的。

验证

打开终端,输入gogo envgo version
如果能识别go命令,则说明安装成功。

编写Hello World

  1. 打开终端,新建test.go文件
  2. 输入如下代码
    1
    2
    3
    4
    5
    package main
    import "fmt"
    func main() {
    fmt.Println("Hello, World!")
    }
  3. 运行go run test.go

02《Go语言入门》编写第一个Go程序Hello, World

https://ganzhixiong.com/p/c02a6a8c/

Author

干志雄

Posted on

2020-12-30

Updated on

2020-12-30

Licensed under

Comments