文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2023-11-17 10:34

关注
目录

拿了一个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.

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项目内容请搜索编程客栈(www.cppcns.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.cppcns.com)!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-人工智能
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯