---
title: "Go命令基础之go run"
author: "cakin24"
date: "2017-08-23T18:49:45Z"
tags: []
categories: ["编程语言"]
slug: "go-go-run"
draft: false
lastmod: "2017-08-23T18:49:45Z"
id: 2390884
url: "https://cakin24.iteye.com/blog/2390884"
---

**一 简介**

用于运行命令源码文件。

只能接受一个命令源码文件以及若干个库源码文件作为文件参数。

其内部执行步骤是：先编译源码文件再运行

![](http://dl2.iteye.com/upload/attachment/0126/6609/fcb9afca-884d-3c8e-a5b3-df6aa6b2ff7b.png)  
 

 

**二 goc2p项目介绍**

1、项目地址

[https://github.com/hyper0x/goc2p](https://github.com/hyper0x/goc2p)

2、项目说明

《go并发编程实战》的示例项目

3、项目安装

用git clone获取项目，并将其根目录作为一个工作区

4、ds命令和pds命令

![](http://dl2.iteye.com/upload/attachment/0126/6611/b1e2c680-e563-37f1-8e4d-ac0f5ca04aa8.png)  
 

 

**三 go run命令实战**

01. `[root@localhost ds]# cd ~`
02. `[root@localhost ~]# ls |grep go`
03. `go1.8.3.linux-amd64.tar.gz`
04. `gobin`
05. `goc2p`
06. `golib`
07. `goproject`
08. `[root@localhost ~]# cd goc2p/src/helper/ds`
09. `[root@localhost ds]# ls`
10. `showds.go`
11. `[root@localhost ds]# go run showds.go`
12. `/root/goc2p/src/helper/ds:`
13. `showds.go`
14. `[root@localhost ds]# go run showds.go -p ~/goc2p/src/helper/`
15. `/root/goc2p/src/helper/:`
16. `ds/`
17. `showds.go`
18. `pds/`
19. `showpds.go`

**四 go run常用标记的使用**

![](http://dl2.iteye.com/upload/attachment/0126/6613/67868732-be9f-385b-a4ee-8e9bf2819694.png)  
 

![](http://dl2.iteye.com/upload/attachment/0126/6615/0492e838-9bdb-37cf-86f9-db7e38c4ff67.png)  
 

 

**五 常用标记案例演示**

01. `[root@localhost ds]# go run showds.go`
02. `/root/goc2p/src/helper/ds:`
03. `showds.go`
04. `[root@localhost ds]# go run -n showds.go`
05. `#`
06. `# command-line-arguments`
07. `#`
08. `mkdir -p $WORK/command-line-arguments/_obj/`
09. `mkdir -p $WORK/command-line-arguments/_obj/exe/`
10. `cd /root/goc2p/src/helper/ds`
11. `/usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/command-line-arguments.a -trimpath $WORK -p main -complete -buildid 8060af8f0adc5e810678187f90009efa4132850d-D _/root/goc2p/src/helper/ds -I $WORK -pack ./showds.go`
12. `cd .`
13. `/usr/local/go/pkg/tool/linux_amd64/link -o $WORK/command-line-arguments/_obj/exe/showds -L $WORK -w -extld=gcc -buildmode=exe -buildid=8060af8f0adc5e810678187f90009efa4132850d $WORK/command-line-arguments.a`
14. `$WORK/command-line-arguments/_obj/exe/showds`
15. `[root@localhost ds]# go run -x showds.go`
16. `WORK=/tmp/go-build846499095`
17. `mkdir -p $WORK/command-line-arguments/_obj/`
18. `mkdir -p $WORK/command-line-arguments/_obj/exe/`
19. `cd /root/goc2p/src/helper/ds`
20. `/usr/local/go/pkg/tool/linux_amd64/compile -o $WORK/command-line-arguments.a -trimpath $WORK -p main -complete -buildid 8060af8f0adc5e810678187f90009efa4132850d-D _/root/goc2p/src/helper/ds -I $WORK -pack ./showds.go`
21. `cd .`
22. `/usr/local/go/pkg/tool/linux_amd64/link -o $WORK/command-line-arguments/_obj/exe/showds -L $WORK -w -extld=gcc -buildmode=exe -buildid=8060af8f0adc5e810678187f90009efa4132850d $WORK/command-line-arguments.a`
23. `$WORK/command-line-arguments/_obj/exe/showds`
24. `/root/goc2p/src/helper/ds:`
25. `showds.go`
26. `[root@localhost ds]# go run -v showds.go`
27. `command-line-arguments`
28. `/root/goc2p/src/helper/ds:`
29. `showds.go`
30. `[root@localhost ds]# go run -work showds.go`
31. `WORK=/tmp/go-build568181490`
32. `/root/goc2p/src/helper/ds:`
33. `showds.go`