Skip to content

Go的基本使用

本文用于记录学习Golang相关知识点。

Go编程语言中文文档中介绍Go:

  • Go编程语言是一个开源项目,它使程序员更具生产力。

  • Go语言具有很强的表达能力,它简洁、清晰而高效。得益于其并发机制,用它编写的程序能够非常有效地利用多核与联网的计算机,其新颖的类型系统则使程序结构变得灵活而模块化。Go代码编译成机器码不仅非常迅速,还具有方便的垃圾收集机制和强大的运行时反射机制。 它是一个快速的、静态类型的编译型语言,感觉却像动态类型的解释型语言。

我们可以简单的理解Go语言解决了其他语言并发执行的痛点,像C语言一样的运行效率高,又兼容了Python语言的开发速度快的特点,Go = C + Python, Go语言被称为"互联网时代的C"。我们不在概念上过多的纠结,直接来开始使用Go这门编程语言。

0. 特别说明

1. 实验环境

本实验使用CentOS7.9作为实验环境。

sh
[root@hellogitlab ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)

安装go前先配置yum源:

sh
[root@hellogitlab ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@hellogitlab ~]# curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

2. 安装和配置GO环境

2.1 安装Golang

  • 使用yum install go -y安装Golang。
sh
[root@hellogitlab ~]# yum install go -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyuncs.com
 * extras: mirrors.aliyuncs.com
 * updates: mirrors.aliyuncs.com
Resolving Dependencies
--> Running transaction check
---> Package golang.x86_64 0:1.20.12-1.el7 will be installed
--> Processing Dependency: golang-bin = 1.20.12-1.el7 for package: golang-1.20.12-1.el7.x86_64
--> Processing Dependency: golang-src = 1.20.12-1.el7 for package: golang-1.20.12-1.el7.x86_64
--> Processing Dependency: openssl-devel for package: golang-1.20.12-1.el7.x86_64
--> Running transaction check
---> Package golang-bin.x86_64 0:1.20.12-1.el7 will be installed
--> Processing Dependency: /usr/bin/gcc for package: golang-bin-1.20.12-1.el7.x86_64
---> Package golang-src.noarch 0:1.20.12-1.el7 will be installed
---> Package openssl-devel.x86_64 1:1.0.2k-26.el7_9 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-26.el7_9 for package: 1:openssl-devel-1.0.2k-26.el7_9.x86_64
--> Processing Dependency: zlib-devel(x86-64) for package: 1:openssl-devel-1.0.2k-26.el7_9.x86_64
--> Processing Dependency: krb5-devel(x86-64) for package: 1:openssl-devel-1.0.2k-26.el7_9.x86_64
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-44.el7 will be installed
--> Processing Dependency: cpp = 4.8.5-44.el7 for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: libmpfr.so.4()(64bit) for package: gcc-4.8.5-44.el7.x86_64
--> Processing Dependency: libmpc.so.3()(64bit) for package: gcc-4.8.5-44.el7.x86_64
---> Package krb5-devel.x86_64 0:1.15.1-55.el7_9 will be installed
--> Processing Dependency: libkadm5(x86-64) = 1.15.1-55.el7_9 for package: krb5-devel-1.15.1-55.el7_9.x86_64
--> Processing Dependency: krb5-libs(x86-64) = 1.15.1-55.el7_9 for package: krb5-devel-1.15.1-55.el7_9.x86_64
--> Processing Dependency: libverto-devel for package: krb5-devel-1.15.1-55.el7_9.x86_64
--> Processing Dependency: libselinux-devel for package: krb5-devel-1.15.1-55.el7_9.x86_64
--> Processing Dependency: libcom_err-devel for package: krb5-devel-1.15.1-55.el7_9.x86_64
--> Processing Dependency: keyutils-libs-devel for package: krb5-devel-1.15.1-55.el7_9.x86_64
---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-19.el7 for package: 1:openssl-1.0.2k-19.el7.x86_64
---> Package openssl-libs.x86_64 1:1.0.2k-26.el7_9 will be an update
---> Package zlib-devel.x86_64 0:1.2.7-21.el7_9 will be installed
--> Processing Dependency: zlib = 1.2.7-21.el7_9 for package: zlib-devel-1.2.7-21.el7_9.x86_64
--> Running transaction check
---> Package cpp.x86_64 0:4.8.5-44.el7 will be installed
---> Package glibc-devel.x86_64 0:2.17-326.el7_9.3 will be installed
--> Processing Dependency: glibc-headers = 2.17-326.el7_9.3 for package: glibc-devel-2.17-326.el7_9.3.x86_64
--> Processing Dependency: glibc = 2.17-326.el7_9.3 for package: glibc-devel-2.17-326.el7_9.3.x86_64
--> Processing Dependency: glibc-headers for package: glibc-devel-2.17-326.el7_9.3.x86_64
---> Package keyutils-libs-devel.x86_64 0:1.5.8-3.el7 will be installed
---> Package krb5-libs.x86_64 0:1.15.1-50.el7 will be updated
---> Package krb5-libs.x86_64 0:1.15.1-55.el7_9 will be an update
---> Package libcom_err-devel.x86_64 0:1.42.9-19.el7 will be installed
---> Package libkadm5.x86_64 0:1.15.1-55.el7_9 will be installed
---> Package libmpc.x86_64 0:1.0.1-3.el7 will be installed
---> Package libselinux-devel.x86_64 0:2.5-15.el7 will be installed
--> Processing Dependency: libsepol-devel(x86-64) >= 2.5-10 for package: libselinux-devel-2.5-15.el7.x86_64
--> Processing Dependency: pkgconfig(libsepol) for package: libselinux-devel-2.5-15.el7.x86_64
--> Processing Dependency: pkgconfig(libpcre) for package: libselinux-devel-2.5-15.el7.x86_64
---> Package libverto-devel.x86_64 0:0.2.5-4.el7 will be installed
---> Package mpfr.x86_64 0:3.1.1-4.el7 will be installed
---> Package openssl.x86_64 1:1.0.2k-19.el7 will be updated
---> Package openssl.x86_64 1:1.0.2k-26.el7_9 will be an update
---> Package zlib.x86_64 0:1.2.7-18.el7 will be updated
---> Package zlib.x86_64 0:1.2.7-21.el7_9 will be an update
--> Running transaction check
---> Package glibc.x86_64 0:2.17-317.el7 will be updated
--> Processing Dependency: glibc = 2.17-317.el7 for package: glibc-common-2.17-317.el7.x86_64
---> Package glibc.x86_64 0:2.17-326.el7_9.3 will be an update
---> Package glibc-headers.x86_64 0:2.17-326.el7_9.3 will be installed
--> Processing Dependency: kernel-headers >= 2.2.1 for package: glibc-headers-2.17-326.el7_9.3.x86_64
--> Processing Dependency: kernel-headers for package: glibc-headers-2.17-326.el7_9.3.x86_64
---> Package libsepol-devel.x86_64 0:2.5-10.el7 will be installed
---> Package pcre-devel.x86_64 0:8.32-17.el7 will be installed
--> Running transaction check
---> Package glibc-common.x86_64 0:2.17-317.el7 will be updated
---> Package glibc-common.x86_64 0:2.17-326.el7_9.3 will be an update
---> Package kernel-headers.x86_64 0:3.10.0-1160.119.1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================================================================================================
 Package                                                                Arch                                                      Version                                                                     Repository                                                  Size
===============================================================================================================================================================================================================================================================================
Installing:
 golang                                                                 x86_64                                                    1.20.12-1.el7                                                               epel                                                       673 k
Installing for dependencies:
 cpp                                                                    x86_64                                                    4.8.5-44.el7                                                                base                                                       5.9 M
 gcc                                                                    x86_64                                                    4.8.5-44.el7                                                                base                                                        16 M
 glibc-devel                                                            x86_64                                                    2.17-326.el7_9.3                                                            updates                                                    1.1 M
 glibc-headers                                                          x86_64                                                    2.17-326.el7_9.3                                                            updates                                                    692 k
 golang-bin                                                             x86_64                                                    1.20.12-1.el7                                                               epel                                                        65 M
 golang-src                                                             noarch                                                    1.20.12-1.el7                                                               epel                                                        12 M
 kernel-headers                                                         x86_64                                                    3.10.0-1160.119.1.el7                                                       updates                                                    9.1 M
 keyutils-libs-devel                                                    x86_64                                                    1.5.8-3.el7                                                                 base                                                        37 k
 krb5-devel                                                             x86_64                                                    1.15.1-55.el7_9                                                             updates                                                    273 k
 libcom_err-devel                                                       x86_64                                                    1.42.9-19.el7                                                               base                                                        32 k
 libkadm5                                                               x86_64                                                    1.15.1-55.el7_9                                                             updates                                                    180 k
 libmpc                                                                 x86_64                                                    1.0.1-3.el7                                                                 base                                                        51 k
 libselinux-devel                                                       x86_64                                                    2.5-15.el7                                                                  base                                                       187 k
 libsepol-devel                                                         x86_64                                                    2.5-10.el7                                                                  base                                                        77 k
 libverto-devel                                                         x86_64                                                    0.2.5-4.el7                                                                 base                                                        12 k
 mpfr                                                                   x86_64                                                    3.1.1-4.el7                                                                 base                                                       203 k
 openssl-devel                                                          x86_64                                                    1:1.0.2k-26.el7_9                                                           updates                                                    1.5 M
 pcre-devel                                                             x86_64                                                    8.32-17.el7                                                                 base                                                       480 k
 zlib-devel                                                             x86_64                                                    1.2.7-21.el7_9                                                              updates                                                     50 k
Updating for dependencies:
 glibc                                                                  x86_64                                                    2.17-326.el7_9.3                                                            updates                                                    3.6 M
 glibc-common                                                           x86_64                                                    2.17-326.el7_9.3                                                            updates                                                     12 M
 krb5-libs                                                              x86_64                                                    1.15.1-55.el7_9                                                             updates                                                    810 k
 openssl                                                                x86_64                                                    1:1.0.2k-26.el7_9                                                           updates                                                    494 k
 openssl-libs                                                           x86_64                                                    1:1.0.2k-26.el7_9                                                           updates                                                    1.2 M
 zlib                                                                   x86_64                                                    1.2.7-21.el7_9                                                              updates                                                     90 k

Transaction Summary
===============================================================================================================================================================================================================================================================================
Install  1 Package  (+19 Dependent packages)
Upgrade             (  6 Dependent packages)

Total download size: 131 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/26): glibc-2.17-326.el7_9.3.x86_64.rpm                                                                                                                                                                                                               | 3.6 MB  00:00:03
(2/26): glibc-devel-2.17-326.el7_9.3.x86_64.rpm                                                                                                                                                                                                         | 1.1 MB  00:00:01
(3/26): glibc-headers-2.17-326.el7_9.3.x86_64.rpm                                                                                                                                                                                                       | 692 kB  00:00:00
(4/26): golang-1.20.12-1.el7.x86_64.rpm                                                                                                                                                                                                                 | 673 kB  00:00:00
(5/26): cpp-4.8.5-44.el7.x86_64.rpm                                                                                                                                                                                                                     | 5.9 MB  00:00:05
(6/26): glibc-common-2.17-326.el7_9.3.x86_64.rpm                                                                                                                                                                                                        |  12 MB  00:00:11
(7/26): keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm                                                                                                                                                                                                      |  37 kB  00:00:00
(8/26): krb5-devel-1.15.1-55.el7_9.x86_64.rpm                                                                                                                                                                                                           | 273 kB  00:00:00
(9/26): krb5-libs-1.15.1-55.el7_9.x86_64.rpm                                                                                                                                                                                                            | 810 kB  00:00:00
(10/26): libcom_err-devel-1.42.9-19.el7.x86_64.rpm                                                                                                                                                                                                      |  32 kB  00:00:00
(11/26): libkadm5-1.15.1-55.el7_9.x86_64.rpm                                                                                                                                                                                                            | 180 kB  00:00:00
(12/26): libmpc-1.0.1-3.el7.x86_64.rpm                                                                                                                                                                                                                  |  51 kB  00:00:00
(13/26): libselinux-devel-2.5-15.el7.x86_64.rpm                                                                                                                                                                                                         | 187 kB  00:00:00
(14/26): libsepol-devel-2.5-10.el7.x86_64.rpm                                                                                                                                                                                                           |  77 kB  00:00:00
(15/26): libverto-devel-0.2.5-4.el7.x86_64.rpm                                                                                                                                                                                                          |  12 kB  00:00:00
(16/26): mpfr-3.1.1-4.el7.x86_64.rpm                                                                                                                                                                                                                    | 203 kB  00:00:00
(17/26): openssl-1.0.2k-26.el7_9.x86_64.rpm                                                                                                                                                                                                             | 494 kB  00:00:00
(18/26): kernel-headers-3.10.0-1160.119.1.el7.x86_64.rpm                                                                                                                                                                                                | 9.1 MB  00:00:09
(19/26): gcc-4.8.5-44.el7.x86_64.rpm                                                                                                                                                                                                                    |  16 MB  00:00:15
(20/26): openssl-devel-1.0.2k-26.el7_9.x86_64.rpm                                                                                                                                                                                                       | 1.5 MB  00:00:01
(21/26): zlib-1.2.7-21.el7_9.x86_64.rpm                                                                                                                                                                                                                 |  90 kB  00:00:00
(22/26): pcre-devel-8.32-17.el7.x86_64.rpm                                                                                                                                                                                                              | 480 kB  00:00:00
(23/26): zlib-devel-1.2.7-21.el7_9.x86_64.rpm                                                                                                                                                                                                           |  50 kB  00:00:00
(24/26): openssl-libs-1.0.2k-26.el7_9.x86_64.rpm                                                                                                                                                                                                        | 1.2 MB  00:00:01
(25/26): golang-src-1.20.12-1.el7.noarch.rpm                                                                                                                                                                                                            |  12 MB  00:00:11
(26/26): golang-bin-1.20.12-1.el7.x86_64.rpm                                                                                                                                                                                                            |  65 MB  00:00:43
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                          2.7 MB/s | 131 MB  00:00:48
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : glibc-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                              1/32
  Updating   : glibc-common-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                       2/32
  Updating   : zlib-1.2.7-21.el7_9.x86_64                                                                                                                                                                                                                                 3/32
  Updating   : krb5-libs-1.15.1-55.el7_9.x86_64                                                                                                                                                                                                                           4/32
  Updating   : 1:openssl-libs-1.0.2k-26.el7_9.x86_64                                                                                                                                                                                                                      5/32
  Installing : mpfr-3.1.1-4.el7.x86_64                                                                                                                                                                                                                                    6/32
  Installing : libmpc-1.0.1-3.el7.x86_64                                                                                                                                                                                                                                  7/32
  Installing : cpp-4.8.5-44.el7.x86_64                                                                                                                                                                                                                                    8/32
  Installing : libkadm5-1.15.1-55.el7_9.x86_64                                                                                                                                                                                                                            9/32
  Installing : zlib-devel-1.2.7-21.el7_9.x86_64                                                                                                                                                                                                                          10/32
  Installing : libcom_err-devel-1.42.9-19.el7.x86_64                                                                                                                                                                                                                     11/32
  Installing : kernel-headers-3.10.0-1160.119.1.el7.x86_64                                                                                                                                                                                                               12/32
  Installing : glibc-headers-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                     13/32
  Installing : glibc-devel-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                       14/32
  Installing : gcc-4.8.5-44.el7.x86_64                                                                                                                                                                                                                                   15/32
  Installing : libverto-devel-0.2.5-4.el7.x86_64                                                                                                                                                                                                                         16/32
  Installing : libsepol-devel-2.5-10.el7.x86_64                                                                                                                                                                                                                          17/32
  Installing : golang-src-1.20.12-1.el7.noarch                                                                                                                                                                                                                           18/32
  Installing : pcre-devel-8.32-17.el7.x86_64                                                                                                                                                                                                                             19/32
  Installing : libselinux-devel-2.5-15.el7.x86_64                                                                                                                                                                                                                        20/32
  Installing : keyutils-libs-devel-1.5.8-3.el7.x86_64                                                                                                                                                                                                                    21/32
  Installing : krb5-devel-1.15.1-55.el7_9.x86_64                                                                                                                                                                                                                         22/32
  Installing : 1:openssl-devel-1.0.2k-26.el7_9.x86_64                                                                                                                                                                                                                    23/32
  Installing : golang-bin-1.20.12-1.el7.x86_64                                                                                                                                                                                                                           24/32
  Installing : golang-1.20.12-1.el7.x86_64                                                                                                                                                                                                                               25/32
  Updating   : 1:openssl-1.0.2k-26.el7_9.x86_64                                                                                                                                                                                                                          26/32
  Cleanup    : 1:openssl-1.0.2k-19.el7.x86_64                                                                                                                                                                                                                            27/32
  Cleanup    : krb5-libs-1.15.1-50.el7.x86_64                                                                                                                                                                                                                            28/32
  Cleanup    : 1:openssl-libs-1.0.2k-19.el7.x86_64                                                                                                                                                                                                                       29/32
  Cleanup    : zlib-1.2.7-18.el7.x86_64                                                                                                                                                                                                                                  30/32
  Cleanup    : glibc-common-2.17-317.el7.x86_64                                                                                                                                                                                                                          31/32
  Cleanup    : glibc-2.17-317.el7.x86_64                                                                                                                                                                                                                                 32/32
  Verifying  : libselinux-devel-2.5-15.el7.x86_64                                                                                                                                                                                                                         1/32
  Verifying  : keyutils-libs-devel-1.5.8-3.el7.x86_64                                                                                                                                                                                                                     2/32
  Verifying  : glibc-headers-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                      3/32
  Verifying  : krb5-devel-1.15.1-55.el7_9.x86_64                                                                                                                                                                                                                          4/32
  Verifying  : pcre-devel-8.32-17.el7.x86_64                                                                                                                                                                                                                              5/32
  Verifying  : zlib-1.2.7-21.el7_9.x86_64                                                                                                                                                                                                                                 6/32
  Verifying  : golang-src-1.20.12-1.el7.noarch                                                                                                                                                                                                                            7/32
  Verifying  : 1:openssl-devel-1.0.2k-26.el7_9.x86_64                                                                                                                                                                                                                     8/32
  Verifying  : libsepol-devel-2.5-10.el7.x86_64                                                                                                                                                                                                                           9/32
  Verifying  : libverto-devel-0.2.5-4.el7.x86_64                                                                                                                                                                                                                         10/32
  Verifying  : 1:openssl-libs-1.0.2k-26.el7_9.x86_64                                                                                                                                                                                                                     11/32
  Verifying  : cpp-4.8.5-44.el7.x86_64                                                                                                                                                                                                                                   12/32
  Verifying  : golang-bin-1.20.12-1.el7.x86_64                                                                                                                                                                                                                           13/32
  Verifying  : zlib-devel-1.2.7-21.el7_9.x86_64                                                                                                                                                                                                                          14/32
  Verifying  : 1:openssl-1.0.2k-26.el7_9.x86_64                                                                                                                                                                                                                          15/32
  Verifying  : glibc-common-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                      16/32
  Verifying  : krb5-libs-1.15.1-55.el7_9.x86_64                                                                                                                                                                                                                          17/32
  Verifying  : glibc-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                             18/32
  Verifying  : gcc-4.8.5-44.el7.x86_64                                                                                                                                                                                                                                   19/32
  Verifying  : libmpc-1.0.1-3.el7.x86_64                                                                                                                                                                                                                                 20/32
  Verifying  : golang-1.20.12-1.el7.x86_64                                                                                                                                                                                                                               21/32
  Verifying  : libkadm5-1.15.1-55.el7_9.x86_64                                                                                                                                                                                                                           22/32
  Verifying  : glibc-devel-2.17-326.el7_9.3.x86_64                                                                                                                                                                                                                       23/32
  Verifying  : mpfr-3.1.1-4.el7.x86_64                                                                                                                                                                                                                                   24/32
  Verifying  : kernel-headers-3.10.0-1160.119.1.el7.x86_64                                                                                                                                                                                                               25/32
  Verifying  : libcom_err-devel-1.42.9-19.el7.x86_64                                                                                                                                                                                                                     26/32
  Verifying  : glibc-common-2.17-317.el7.x86_64                                                                                                                                                                                                                          27/32
  Verifying  : zlib-1.2.7-18.el7.x86_64                                                                                                                                                                                                                                  28/32
  Verifying  : 1:openssl-1.0.2k-19.el7.x86_64                                                                                                                                                                                                                            29/32
  Verifying  : 1:openssl-libs-1.0.2k-19.el7.x86_64                                                                                                                                                                                                                       30/32
  Verifying  : glibc-2.17-317.el7.x86_64                                                                                                                                                                                                                                 31/32
  Verifying  : krb5-libs-1.15.1-50.el7.x86_64                                                                                                                                                                                                                            32/32

Installed:
  golang.x86_64 0:1.20.12-1.el7

Dependency Installed:
  cpp.x86_64 0:4.8.5-44.el7                          gcc.x86_64 0:4.8.5-44.el7                     glibc-devel.x86_64 0:2.17-326.el7_9.3      glibc-headers.x86_64 0:2.17-326.el7_9.3      golang-bin.x86_64 0:1.20.12-1.el7           golang-src.noarch 0:1.20.12-1.el7
  kernel-headers.x86_64 0:3.10.0-1160.119.1.el7      keyutils-libs-devel.x86_64 0:1.5.8-3.el7      krb5-devel.x86_64 0:1.15.1-55.el7_9        libcom_err-devel.x86_64 0:1.42.9-19.el7      libkadm5.x86_64 0:1.15.1-55.el7_9           libmpc.x86_64 0:1.0.1-3.el7
  libselinux-devel.x86_64 0:2.5-15.el7               libsepol-devel.x86_64 0:2.5-10.el7            libverto-devel.x86_64 0:0.2.5-4.el7        mpfr.x86_64 0:3.1.1-4.el7                    openssl-devel.x86_64 1:1.0.2k-26.el7_9      pcre-devel.x86_64 0:8.32-17.el7
  zlib-devel.x86_64 0:1.2.7-21.el7_9

Dependency Updated:
  glibc.x86_64 0:2.17-326.el7_9.3           glibc-common.x86_64 0:2.17-326.el7_9.3           krb5-libs.x86_64 0:1.15.1-55.el7_9           openssl.x86_64 1:1.0.2k-26.el7_9           openssl-libs.x86_64 1:1.0.2k-26.el7_9           zlib.x86_64 0:1.2.7-21.el7_9

Complete!
[root@hellogitlab ~]#

2.2 查看Golang版本

sh
[root@hellogitlab ~]# go version
go version go1.20.12 linux/amd64

2.3 查看go命令的帮助信息

sh
[root@hellogitlab ~]# go
Go is a tool for managing Go source code.

Usage:

        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         add dependencies to current module and install them
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        work        workspace maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildconstraint build constraints
        buildmode       build modes
        c               calling between Go and C
        cache           build and test caching
        environment     environment variables
        filetype        file types
        go.mod          the go.mod file
        gopath          GOPATH environment variable
        gopath-get      legacy GOPATH go get
        goproxy         module proxy protocol
        importpath      import path syntax
        modules         modules, module versions, and more
        module-get      module-aware go get
        module-auth     module authentication using go.sum
        packages        package lists and patterns
        private         configuration for downloading non-public code
        testflag        testing flags
        testfunc        testing functions
        vcs             controlling version control with GOVCS

Use "go help <topic>" for more information about that topic.

[root@hellogitlab ~]#

2.4 go安装信息检查

sh
# 查看go处于什么位置
[root@hellogitlab ~]# whereis go
go: /usr/bin/go 
# 查看/usr/bin/go的详细信息,可以看到最终指向了/usr/lib/golang/bin/go
[root@hellogitlab ~]# ls -lah /usr/bin/go
lrwxrwxrwx. 1 root root 22 Nov  7 21:16 /usr/bin/go -> /usr/lib/golang/bin/go
[root@hellogitlab ~]# ls -lah /usr/lib/golang/bin/go
-rwxr-xr-x. 1 root root 16M Mar  6  2024 /usr/lib/golang/bin/go
# golang的安装目录
[root@hellogitlab ~]# ls -lah /usr/lib/golang/
total 16K
drwxr-xr-x.  7 root root   76 Nov  7 21:16 .
dr-xr-xr-x. 29 root root 4.0K Nov  7 21:16 ..
drwxr-xr-x.  2 root root 4.0K Nov  7 21:16 api
drwxr-xr-x.  2 root root   29 Nov  7 21:16 bin
drwxr-xr-x.  3 root root   18 Nov  7 21:16 lib
drwxr-xr-x.  4 root root   33 Nov  7 21:16 pkg
drwxr-xr-x. 48 root root 4.0K Nov  7 21:16 src
-rw-r--r--.  1 root root    9 Dec  6  2023 VERSION
[root@hellogitlab ~]#

我们后续使用普通账户meizhaohui来进行go语言相关命令的操作。

2.5 配置Go的工作空间

GO代码必须在工作空间内。工作空间是一个目录,其中包含三个子目录:

  • src 里面每一个子目录,就是一个包。包内是Go的源码文件
  • pkg 编译后生成的,包的目标文件
  • bin 生成的可执行文件

使用普通账户meizhaohui创建Go的工作空间目录:

sh
# 查看我是谁
[meizhaohui@hellogitlab ~]$ whoami
meizhaohui
# 查看当前文件夹中的文件
[meizhaohui@hellogitlab ~]$ ls
# 创建工作空间目录
[meizhaohui@hellogitlab ~]$ mkdir -p data/go_data
[meizhaohui@hellogitlab ~]$ ls -lsh data/
total 0
0 drwxrwxr-x. 5 meizhaohui meizhaohui 39 Nov  7 21:37 go_data
# 在工作空间目录下面创建子目录
[meizhaohui@hellogitlab ~]$ mkdir -p data/go_data/{src,bin,pkg}
[meizhaohui@hellogitlab ~]$ ls -lah data/go_data/
total 0
drwxrwxr-x. 5 meizhaohui meizhaohui 39 Nov  7 21:37 .
drwxrwxr-x. 3 meizhaohui meizhaohui 21 Nov  7 21:37 ..
drwxrwxr-x. 2 meizhaohui meizhaohui  6 Nov  7 21:37 bin
drwxrwxr-x. 2 meizhaohui meizhaohui  6 Nov  7 21:37 pkg
drwxrwxr-x. 2 meizhaohui meizhaohui  6 Nov  7 21:37 src
[meizhaohui@hellogitlab ~]$

2.6 配置PATHGOPATHGOROOTGOBIN等环境变量

~/.bashrc文件中增加以下内容:

# Golang environment settings
export GOROOT=/usr/lib/golang # golang install path
export GOPATH=/home/meizhaohui/data/go_data # golang workspace
export GOBIN=${GOPATH}/bin # golang exe files
export PATH=${GOBIN}:${PATH}

保存后,source ~/.bashrc加载配置文件,并查看以上设置的Go语言的环境变量:

sh
[meizhaohui@hellogitlab ~]$ source ~/.bashrc
[meizhaohui@hellogitlab ~]$ echo $GOPATH
/home/meizhaohui/data/go_data
[meizhaohui@hellogitlab ~]$ echo $GOBIN
/home/meizhaohui/data/go_data/bin
[meizhaohui@hellogitlab ~]$ echo $GOROOT
/usr/lib/golang
[meizhaohui@hellogitlab ~]$ echo $PATH
/home/meizhaohui/data/go_data/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/meizhaohui/.local/bin:/home/meizhaohui/bin

查看到以上信息,说环境变量配置成功。

2.7 开启Go module和代理

默认情况下,使用go get下载包时会非常慢,需要开启Go module和代理,加快我们包的安装速度。

为了今后中国的Go语言开发者能更好地进行开发,七牛云推出了非营利性项目 goproxy.cn,其目标是为中国和世界上其他地方的 Gopher 们提供一个免费的、可靠的、持续在线的且经过 CDN 加速的模块代理。

参考:

~/.bashrc文件中增加以下内容:

sh
export GO111MODULE=on
export GOPROXY=https://goproxy.cn

保存后,source ~/.bashrc加载配置文件,并查看以上设置的Go语言的环境变量:

sh
[meizhaohui@hellogitlab ~]$ echo $GO111MODULE
on
[meizhaohui@hellogitlab ~]$ echo $GOPROXY 
https://goproxy.cn

# 测试安装包
# go get 跟go.mod 紧密相关,它需要go.mod ,并且每次更改都会记录在go.mod中,当你在一个没有go.mod的路径下(指的是此路径,此路径的父路径也没有)使用go get 会提示如下错误
[meizhaohui@hellogitlab ~]$ go get -v github.com/stamblerre/gocode
go: go.mod file not found in current directory or any parent directory.
        'go get' is no longer supported outside a module.
        To build and install a command, use 'go install' with a version,
        like 'go install example.com/cmd@latest'
        For more information, see https://golang.org/doc/go-get-install-deprecation
        or run 'go help get' or 'go help install'.
[meizhaohui@hellogitlab ~]$

此时,使用go install代替下载包:

sh
# go install 用于下载包,两者都是一样的,如果包的后面带上版本那么就是指定版本
[meizhaohui@hellogitlab ~]$ go install github.com/stamblerre/gocode
go: 'go install' requires a version when current directory is not in a module
        Try 'go install github.com/stamblerre/gocode@latest' to install the latest version
[meizhaohui@hellogitlab ~]$ echo $?
1
[meizhaohui@hellogitlab ~]$ go install github.com/stamblerre/gocode@latest
go: downloading github.com/stamblerre/gocode v1.0.0
go: downloading github.com/keegancsmith/rpc v1.1.0
go: downloading golang.org/x/tools v0.0.0-20191030062658-86caa796c7ab
[meizhaohui@hellogitlab ~]$ echo $?
0
[meizhaohui@hellogitlab ~]$

在使用 go install的时候其实是忽略 go.mod 的,所以go install 跟go.mod 没有任何关系,也不会记录go.mod中,它的作用就是下载包。

3. 第一个Go程序,你好世界

3.1 编写Go代码文件

以最经典的Hello world程序为例,编写第一个Go代码文件。

我们切换到$GOPATH/src目录中,创建第一个Go代码文件。

sh
[meizhaohui@hellogitlab ~]$ cd $GOPATH/src
[meizhaohui@hellogitlab src]$ pwd
/home/meizhaohui/data/go_data/src
# 使用vim编写hello.go文件,保存后查看内容如下
[meizhaohui@hellogitlab src]$ cat hello.go
package main

import "fmt"

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

由于后期经常会切换到$GOPATH/src这个目录来编写代码,我们可以在~/.bashrc中设置一个快捷命令:

sh
alias cdgo='cd ${GOPATH}/src && pwd'

修改后使用source ~/.bashrc使快捷命令生效。

查看效果:

sh
[meizhaohui@hellogitlab ~]$ cdgo
/home/meizhaohui/data/go_data/src

3.2 编译Go程序go build

  • 使用go build filename.go编译go程序,如go build hello.go编译生成可执行文件hello
sh
[meizhaohui@hellogitlab src]$ go build hello.go 
[meizhaohui@hellogitlab src]$ ls
hello  hello.go
# 运行可执行文件hello
[meizhaohui@hellogitlab src]$ ./hello
Hello,World

3.3 编译并直接运行Go程序go run

  • 使用go run filename.go编译并直接运行go程序,如go build hello.go编译时不生成可执行文件,直接显示运行结果。
sh
[meizhaohui@hellogitlab src]$ rm -f hello
[meizhaohui@hellogitlab src]$ ls
hello.go
[meizhaohui@hellogitlab src]$ go run hello.go
Hello,World
[meizhaohui@hellogitlab src]$ ls
hello.go

3.4 编译并安装go install

  • 对go源码文件go install,会进行编译+链接+生成可执行文件, 会在$GOBIN目录下生成可执行文件。
sh
# 编译并安装
[meizhaohui@hellogitlab src]$ go install hello.go
# 查看当前目录下是否生成可执行文件
[meizhaohui@hellogitlab src]$ ls 
hello.go
# 查看bin目录下是否生成可执行文件
[meizhaohui@hellogitlab src]$ ls $GOBIN
hello
# 查看hello可执行文件的位置
[meizhaohui@hellogitlab src]$ whereis hello
hello: /home/meizhaohui/data/go_data/bin/hello
# 在任何目录执行hello,会打印出世界你好
[meizhaohui@hellogitlab src]$ hello
Hello,World
[meizhaohui@hellogitlab src]$ cd
[meizhaohui@hellogitlab ~]$ hello
Hello,World

golang的安装和简单使用就到此了。

本首页参考 https://notes.fe-mm.com/ 配置而成