Linux环境下编译并运行go项目的全过程
作者:临风赏月
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项目内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!