Linux环境下编译并运行go项目的全过程

 更新时间:2023年11月16日 11:18:52   作者:临风赏月  
Go语言是Google的开源编程语言,广泛应用于云计算、分布式系统开发等领域,在Linux上也有大量的应用场景,这篇文章主要给大家介绍了关于Linux环境下编译并运行go项目的相关资料,需要的朋友可以参考下

拿了一个GitHub的go采集namenode指标的项目,修改了一下,本地调测正常,记录一下在Linux环境编译并运行的过程。

1.安装go环境

[root@hadoop1011 ~]# yum install go -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package golang.x86_64 0:1.18.9-1.el7 will be installed
--> Processing Dependency: golang-bin = 1.18.9-1.el7 for package: golang-1.18.9-1.el7.x86_64
--> Processing Dependency: golang-src = 1.18.9-1.el7 for package: golang-1.18.9-1.el7.x86_64
--> Running transaction check
---> Package golang-bin.x86_64 0:1.18.9-1.el7 will be installed
---> Package golang-src.noarch 0:1.18.9-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================
 Package                                         Arch                                        Version                                             Repository                                 Size
=================================================================================================================================================================================================
Installing:
 golang                                          x86_64                                      1.18.9-1.el7                                        epel                                      685 k
Installing for dependencies:
 golang-bin                                      x86_64                                      1.18.9-1.el7                                        epel                                      105 M
 golang-src                                      noarch                                      1.18.9-1.el7                                        epel                                      9.1 M

Transaction Summary
=================================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 115 M
Installed size: 420 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): golang-1.18.9-1.el7.x86_64.rpm                                                                                                                                     | 685 kB  00:00:00     
(2/3): golang-src-1.18.9-1.el7.noarch.rpm                                                                                                                                 | 9.1 MB  00:00:02     
(3/3): golang-bin-1.18.9-1.el7.x86_64.rpm                                                                                                                                 | 105 MB  00:00:14     
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                            8.2 MB/s | 115 MB  00:00:14     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : golang-src-1.18.9-1.el7.noarch                                                                                                                                                1/3 
  Installing : golang-1.18.9-1.el7.x86_64                                                                                                                                                    2/3 
  Installing : golang-bin-1.18.9-1.el7.x86_64                                                                                                                                                3/3 
  Verifying  : golang-bin-1.18.9-1.el7.x86_64                                                                                                                                                1/3 
  Verifying  : golang-1.18.9-1.el7.x86_64                                                                                                                                                    2/3 
  Verifying  : golang-src-1.18.9-1.el7.noarch                                                                                                                                                3/3 

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

Dependency Installed:
  golang-bin.x86_64 0:1.18.9-1.el7                                                                golang-src.noarch 0:1.18.9-1.el7                                                               

Complete!

2.尝试编译,发现失败

[root@hadoop1011 ~]# go build namenode_exporter.go 
namenode_exporter.go:9:2: no required module provides package github.com/prometheus/client_golang/prometheus: go.mod file not found in current directory or any parent directory; see 'go help mo
dules'namenode_exporter.go:10:2: no required module provides package github.com/prometheus/log: go.mod file not found in current directory or any parent directory; see 'go help modules'

3. 更改GOPROXY依赖下载代理为国内的地址

[root@hadoop1011 ~]# go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/golang"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.9"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build820474747=/tmp/go-build -gno-record-gcc-switches"

[root@hadoop1011 go_pkg]# go env -w GOPROXY=https://mirrors.aliyun.com/goproxy,direct

4.编译项目

[root@hadoop1011 go_pkg]# go build namenode_exporter.go 
go: downloading github.com/prometheus/log v0.0.0-20151026012452-9a3136781e1f
go: downloading github.com/prometheus/client_golang v0.8.0
go: downloading github.com/Sirupsen/logrus v1.0.6
go: downloading github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273
go: downloading github.com/golang/protobuf v1.2.0
go: downloading github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e
go: downloading github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973
go: downloading github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910
go: downloading golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8
go: downloading golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
[root@hadoop1011 go_pkg]# ls
go.mod  go.sum  LICENSE  namenode_exporter  namenode_exporter.exe  namenode_exporter.go  README.md  resourcemanager_exporter.go
[root@hadoop1011 go_pkg]# ll
total 20936
-rw-r--r-- 1 root root     1033 Apr 19 19:43 go.mod
-rw-r--r-- 1 root root     4526 Apr 19 19:43 go.sum
-rw-r--r-- 1 root root    11357 Apr 19 19:43 LICENSE
-rwxr-xr-x 1 root root 10570414 Apr 19 19:56 namenode_exporter
-rw-r--r-- 1 root root 10808832 Apr 19 19:30 namenode_exporter.exe
-rw-r--r-- 1 root root    13704 Apr 19 19:43 namenode_exporter.go
-rw-r--r-- 1 root root      944 Apr 19 19:43 README.md
-rw-r--r-- 1 root root     9586 Apr 19 19:43 resourcemanager_exporter.go

5.启动进程

[root@hadoop1011 go_pkg]# nohup ./namenode_exporter &
[1] 4361
[root@hadoop1011 go_pkg]# nohup: ignoring input and appending output to ‘nohup.out'

[root@hadoop1011 go_pkg]# ll
total 20940
-rw-r--r-- 1 root root     1033 Apr 19 19:43 go.mod
-rw-r--r-- 1 root root     4526 Apr 19 19:43 go.sum
-rw-r--r-- 1 root root    11357 Apr 19 19:43 LICENSE
-rwxr-xr-x 1 root root 10570414 Apr 19 19:56 namenode_exporter
-rw-r--r-- 1 root root 10808832 Apr 19 19:30 namenode_exporter.exe
-rw-r--r-- 1 root root    13704 Apr 19 19:43 namenode_exporter.go
-rw------- 1 root root      108 Apr 19 19:56 nohup.out
-rw-r--r-- 1 root root      944 Apr 19 19:43 README.md
-rw-r--r-- 1 root root     9586 Apr 19 19:43 resourcemanager_exporter.go
[root@hadoop1011 go_pkg]# cat nohup.out 
time="2023-04-19T19:56:22+08:00" level=info msg="Starting Server: :9070" file=namenode_exporter.go line=395
[root@hadoop1011 go_pkg]# ll
total 20940
-rw-r--r-- 1 root root     1033 Apr 19 19:43 go.mod
-rw-r--r-- 1 root root     4526 Apr 19 19:43 go.sum
-rw-r--r-- 1 root root    11357 Apr 19 19:43 LICENSE
-rwxr-xr-x 1 root root 10570414 Apr 19 19:56 namenode_exporter
-rw-r--r-- 1 root root 10808832 Apr 19 19:30 namenode_exporter.exe
-rw-r--r-- 1 root root    13704 Apr 19 19:43 namenode_exporter.go
-rw------- 1 root root      108 Apr 19 19:56 nohup.out
-rw-r--r-- 1 root root      944 Apr 19 19:43 README.md
-rw-r--r-- 1 root root     9586 Apr 19 19:43 resourcemanager_exporter.go

6.检测进程是否存在

[root@hadoop1011 go_pkg]# ss -tlnp|grep 9070
LISTEN     0      128         :::9070                    :::*                   users:(("namenode_export",pid=4361,fd=3))
[root@hadoop1011 go_pkg]# 

总结 

到此这篇关于Linux环境下编译并运行go项目的文章就介绍到这了,更多相关Linux编译运行go项目内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于Go语言简单实现事件管理器

    基于Go语言简单实现事件管理器

    在编程中,事件管理器是一种常见的工具,用于通过通知来触发操作,本文将介绍一个简单的Go事件管理器的实现,并通过异步改进提高其性能,感兴趣的可以了解下
    2023-11-11
  • Go http client 连接池不复用的问题

    Go http client 连接池不复用的问题

    这篇文章主要介绍了Go http client 连接池不复用的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Golang实现获取与解析命令行参数

    Golang实现获取与解析命令行参数

    这篇文章主要为大家详细介绍了Golang如何实现获取与解析命令行参数,文中的示例代码讲解详细,具有一定的借鉴价值,需要的小伙伴可以参考一下
    2024-01-01
  • GO语言实现批量压缩图片和水印

    GO语言实现批量压缩图片和水印

    这篇文章主要介绍了GO语言实现批量压缩图片和水印,主要用到了github.com/nfnt/resize这个第三方库,仅仅支持JPG图片格式,有相同需求的小伙伴参考下吧。
    2015-03-03
  • Go中时间与时区问题的深入讲解

    Go中时间与时区问题的深入讲解

    go语言中如果不设置指定的时区,通过time.Now()获取到的就是本地时区,下面这篇文章主要给大家介绍了关于Go中时间与时区问题的相关资料,需要的朋友可以参考下
    2021-12-12
  • 浅析Go中序列化与反序列化的基本使用

    浅析Go中序列化与反序列化的基本使用

    序列化是指将对象转换成字节流,从而存储对象或将对象传输到内存、数据库或文件的过程,反向过程称为“反序列化”。本文主要介绍了Go中序列化与反序列化的基本使用,需要的可以参考一下
    2023-04-04
  • Go语言单元测试的实现及用例

    Go语言单元测试的实现及用例

    在日常开发中,我们通常需要针对现有的功能进行单元测试,以验证开发的正确性,本文主要介绍了Go语言单元测试的实现及用例,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • 解决Goland 同一个package中函数互相调用的问题

    解决Goland 同一个package中函数互相调用的问题

    这篇文章主要介绍了解决Goland 同一个package中函数互相调用的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-05-05
  • Golang实现微信公众号后台接入的示例代码

    Golang实现微信公众号后台接入的示例代码

    这篇文章主要介绍了Golang实现微信公众号后台接入的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • Gin框架自带参数校验的使用详解

    Gin框架自带参数校验的使用详解

    这篇文章主要为大家详细介绍了如何使用Gin框架自带的参数校验,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解下
    2023-09-09

最新评论