Linux运行go文件

Linux下运行go程序

第一种:

1.查找 gopath 目录

[root@www ~]# cd /etc
[root@www etc]# vim /etc/profile
export GOROOT=/usr/go

2.定位到 gopath 文件下,将 .go文件放到文件夹下(通过WinSCP / XShell )

[root@www etc]# cd /usr/go/src/mylearn/test1
[root@www test1]# ll
total 1
-rw-r--r--. 1 root root      73 Dec 31 13:46 main.go

3.在linux 下生成go文件

[root@www test1]# go build main.go
[root@www test1]# ll
total 1868
-rwxrwxrwx. 1 root root 1906945 Dec 31 13:55 main
-rw-r--r--. 1 root root      73 Dec 31 13:46 main.go

4.将main文件赋予可执行权限

chmod 777 main

5.运行./main,程序启动

[root@www test1]# ./main
Hello Linux!

6.在后台启动程序,

[root@www test1]# nohup ./main &
[1] 11722
[root@www test1]# nohup: ignoring input and appending output to ‘nohup.out’

证明运行成功,同时把程序运行的输出信息放到当前目录的 nohup.out 文件中去。

[root@www test1]# ll
total 1872
-rwxrwxrwx. 1 root root 1906945 Dec 31 13:55 main
-rw-r--r--. 1 root root      73 Dec 31 13:46 main.go
-rw-------. 1 root root      26 Dec 31 14:07 nohup.out

7.查看程序是否正常运行

ps aux|grep main

程序正常运行在pid上,go应用部署在linux服务器上运行已完成。

第二种-本地编译:

windows下 cmd控制台到main.go文件目录下

set GOARCH=amd64

set GOOS=linux

go build main.go

会生成一个没有后缀的二进制文件

main

将该文件放入linux系统某个文件夹下

赋予权限

chmod 777 main

最后执行 ./main 就行了。

发布了4 篇原创文章 · 获赞 0 · 访问量 77

猜你喜欢

转载自blog.csdn.net/u013328965/article/details/103987944