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原子级内存操作实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-02-02
  • Golang二维切片初始化的实现

    Golang二维切片初始化的实现

    这篇文章主要介绍了Golang二维切片初始化的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • Go语言基础学习之map的示例详解

    Go语言基础学习之map的示例详解

    哈希表是常见的数据结构,有的语言会将哈希称作字典或者映射,在Go中,哈希就是常见的数据类型map,本文就来聊聊Golang中map的相关知识吧
    2023-04-04
  • go打包aar及flutter调用aar流程详解

    go打包aar及flutter调用aar流程详解

    这篇文章主要为大家介绍了go打包aar及flutter调用aar流程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • 一文吃透Go的内置RPC原理

    一文吃透Go的内置RPC原理

    这篇文章主要为大家详细介绍了Go语言中内置RPC的原理。说起 RPC 大家想到的一般是框架,Go 作为编程语言竟然还内置了 RPC,着实让我有些吃鲸,本文就来一起聊聊吧
    2023-03-03
  • 在Go中创建自定义错误的方式总结

    在Go中创建自定义错误的方式总结

    在程序开发中错误处理是至关重要的,下面这篇文章主要给大家介绍了关于在Go中创建自定义错误的方式,文中通过代码介绍的非常详细,对大家学习或者使用Go具有一定的参考借鉴价值,需要的朋友可以参考下
    2024-01-01
  • Golang中map数据类型的使用方法

    Golang中map数据类型的使用方法

    这篇文章主要介绍了Golang中map数据类型的使用方法,文章围绕主题展开详细的内容戒杀,具有一定的参考价值,需要的朋友可以参考一下
    2022-09-09
  • Go语言单线程运行也会有的并发问题解析

    Go语言单线程运行也会有的并发问题解析

    这篇文章主要为大家介绍了Go语言单线程运行的并发问题解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • 详解Go中的高效切片拼接和Go1.22提供的新方法

    详解Go中的高效切片拼接和Go1.22提供的新方法

    在 Go 语言中,切片拼接是一项常见的操作,但如果处理不当,可能会导致性能问题或意外的副作用,本文将详细介绍几种高效的切片拼接方法,希望对大家有所帮助
    2024-01-01
  • 如何用golang运行第一个项目

    如何用golang运行第一个项目

    这篇文章主要介绍了如何用golang运行第一个项目,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03

最新评论