Go命令行操作

Go学习路径的Go命令模块,参考资料:Go帮助文档

在控制台使用Go命令可以打印出帮助文档,英文不好的同学可以看看我写的这个笔记,没啥水平我也在学,仅供参考,发现错误欢迎指正!

1.go bug

> Bug打开默认浏览器并启动新的错误报告。 > 该报告包含有用的系统信息。

emm…这个命令我试了一下,并不是给你debug用的,而是你执行了go bug后他会打开浏览器并跳转到go的github主页的issues页面,就是说如果你发现了go语言的bug,用这个命令就可以快速的通知go开发团队。orz…orz…

2.go build

>Build compiles the packages named by the import paths, >along with their dependencies, but it does not install the results.

你可以对一堆包进行编译

If the arguments to build are a list of .go files, build treats
them as a list of source files specifying a single package.

你也可以编译一堆go文件

When compiling a single main package, build writes the resulting executable to an output file named after the first source file (‘go build ed.go rx.go’ writes ‘ed’ or ‘ed.exe’) or the source code directory (‘go build unix/sam’ writes ‘sam’ or ‘sam.exe’).The ‘.exe’ suffix is added when writing a Windows executable.

如果你只编译main包,最后可执行文件的名字就是这个包名,如果你编译一堆文件,最后可执行文件的名字就和第一个go文件同名

When compiling multiple packages or a single non-main package,build compiles the packages but discards the resulting object,serving only as a check that the packages can be built.

如果你编译多个包或者单个非main包,不会生成可执行文件

When compiling packages, build ignores files that end in ‘_test.go’.

如果你的go文件以_开头,编译时会忽略它

The -o flag, only allowed when compiling a single package, forces build to write the resulting executable or object to the named output file, instead of the default behavior described in the last two paragraphs.

如果你只编译一个包,可以用-o参数指定生成的可执行文件名

The -i flag installs the packages that are dependencies of the target.

-i参数安装目标依赖包

The build flags are shared by the build, clean, get, install, list, run, and test commands:

还有一些参数build clean get install list run test命令用法都一样,那这个就在文章最后写吧

这还有个我在网上看到的关于go build命令的操作,它可以编译成不同平台的可执行文件。比如说这个命令GOOS=linux GOARCH=amd64 go build ...可以生成linux 64位程序。

GOOSGOARCH
darwinarm
freebsdarm64
linux386
windowsamd64
androidppc64
dragonflyppc64le
netbsdmips64
openbsdmips64le
plan9s390x
solaris

3.go clean

>Clean removes object files from package source directories. >The go command builds most objects in a temporary directory,so go clean is mainly concerned with object files left by other tools or by manual invocations of go build.

清理目录,只留源码,下面是参数列表

参数含义
- i删掉go install安装的
- n把要执行的删除命令打印出来,但不执行
- r删掉你导入的包
- x执行- n打印的那些命令
- cache删掉所有缓存
- testcache删掉test缓存
- modcache删掉模块下载缓存

4.go doc

> Doc prints the documentation comments associated with the item identified by itsarguments (a package, const, func, type, var, method, or struct field) followed by a one-line summary of each of the first-level items "under" that item (package-level declarations for a package, methods for a type,etc.).

打印实体的文档注释,即package,const,func,type,var,method,struct这些东西。

Doc accepts zero, one, or two arguments. Given no arguments, that is, when run as

go doc

it prints the package documentation for the package in the current directory.
If the package is a command (package main), the exported symbols of the package
are elided from the presentation unless the -cmd flag is provided.

go doc命令可以接0 - 2个参数,如果不加参数会把目录中所有实体列出

下面的文档太长我自己总结一下:

一个参数可以指定你要的实体,跟在go doc命令后面,你就可以之查看它了。两个参数是第一个参数指定包,第二个参数指定你要看的实体

下面是一些参数:

参数含义
- all打印包的所有实体
- c区分大小写
- cmd打印main中实体,默认不打印
- src打印完整源码定义
- u打印非导出实体即首字母小写,默认不打印

5.go env

>Env prints Go environment information. > >By default env prints information as a shell script (on Windows, a batch file). If one or more variable names is given as arguments, env prints the value of each named variable on its own line.

打印go环境的一些参数,你也可以在后面指定参数名

The -json flag prints the environment in JSON format instead of as a shell script.

也可以加- json参数以json格式打印

6.go fix

> Fix runs the Go fix command on the packages named by the import paths.

如果go版本升级了,这个命令可以让你的代码也更新一下版本

7.go fmt

>Fmt runs the command 'gofmt -l -w' on the packages named by the import paths. It prints the names of the files that are modified. > >For more about gofmt, see 'go doc cmd/gofmt'. >For more about specifying packages, see 'go help packages'. > >The -n flag prints commands that would be executed. >The -x flag prints commands as they are executed.

go fmtgofmt -l -w一样用来格式化你的代码,下面是一些参数

参数含义
- n打印要执行的命令
- x打印被执行的命令

8.go generate

>Generate runs commands described by directives within existing files. Those commands can run any process but the intent is to create or update Go source files. > >Go generate is never run automatically by go build, go get, go test, and so on. It must be run explicitly. > >Go generate scans the file for directives, which are lines of the form, > >​ //go:generate command argument...

你可以在你的go文件中加类似//go:generate ...之类的注释,使用go generate可以扫描到它们并执行它后面指令

Go generate sets several variables when it runs the generator:

使用go generate有一些变量可以给我们用

变量含义
$GOARCH系统架构
$GOOS系统
$GOFILE文件名
$GOLINE当前命令在文件中的行号
$GOPACKAGE当前命令所在的包名
$DOLLAR$标志

go generate还有一些参数可以用

参数含义
- run正则表达式,执行匹配到的命令
- v打印被处理的包名和源文件名
- n打印命令但不执行
- x打印命令且执行

9.go get

>Get downloads the packages named by the import paths, along with their dependencies. It then installs the named packages, like 'go install'.

下载包及其依赖,然后用go install安装他们

参数含义
- d下载后不安装
- u更新包
- fix下载后做一下适配
- insecure允许不安全的下载
- t连测试包也下
- v启用详细进度和调试输出

10.go install

>Install compiles and installs the packages named by the import paths. > >The -i flag installs the dependencies of the named packages as well.

安装指定的包,加 - i 参数可以安装其依赖项

11.go list

> List lists the named packages, one per line.The most commonly-used flags are -f and -json, which control the form of the output printed for each package.

go list命令输出指定包信息,最常用的是 - f,- json 参数

加上json可以json格式显示完整信息,-f可以显示指定信息

12.go mod

不会...看不懂...建议百度...

13.go run

编译并执行go文件,与`go build`一样,但它不留存可执行文件

14.go test

>'Go test' recompiles each package along with any files with names matching the file pattern "*_test.go". >These additional files can contain test functions, benchmark functions, and example functions. See 'go help testfunc' for more. >Each listed package causes the execution of a separate test binary. >Files whose names begin with "_" (including "_test.go") or "." are ignored._ > >Test files that declare a package with the suffix "_test" will be compiled as a separate package, and then linked and run with the main test binary. > >The go tool will ignore a directory named "testdata", making it available to hold ancillary data needed by the tests.

执行go test会编译运行那些xxx_test.go文件,而这些文件在正常编译时会被忽略,且以testdata命名的目录也会被忽略,使其可用于保存测试所需的辅助数据

15.go tool

>Tool runs the go tool command identified by the arguments. >With no arguments it prints the list of known tools. > >The -n flag causes tool to print the command that would be executed but not execute it.

go tool命令打印你现在有什么工具,-n参数使工具打印将要执行但不执行它的命令。

16.go version

> Version prints the Go version, as reported by runtime.Version.

这个很简单233,就是go version打印你的go语言版本

17.go vet

`go vet`是一个用于检查Go语言源码中静态错误的简单工具

支持-n打印不执行与-x打印并执行两个参数


最后是build, clean, get, install, list, run, test这些命令通用的参数表

参数含义
-a强制重建已经是最新的软件包。
-n打印命令但不执行
-p n可以并行运行的程序数
-race启用数据竞争检测仅支持64位
-msan启用内存清理程序互操作,仅在linux
-v编译时打印包名
-work打印临时目录名,且退出时不删除
-x打印执行的命令

欢迎访问我的主页

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Go 中,可以使用 `flag` 包来解析命令行选项。 首先,在程序中导入 `flag` 包: ``` import "flag" ``` 然后,使用 `flag.Bool` 函数定义一个 `-d` 选项。这个函数接受两个参数:选项的名字和选项的默认值。在这个例子中,我们将选项的默认值设为 `false`,表示程序默认不会进入后台执行。 ``` var d = flag.Bool("d", false, "run program in the background") ``` 接下来,调用 `flag.Parse()` 函数来解析命令行选项。 ``` flag.Parse() ``` 最后,在程序中使用 `*d` 来判断是否有 `-d` 选项。如果有,就执行后台执行的操作;如果没有,就继续执行剩余的程序逻辑。 ``` if *d { // Run program in the background } else { // Continue with rest of the program } ``` 总的来说,你可以按照以下步骤来设置程序判断是否有 `-d` 选项: 1. 导入 `flag` 包 2. 使用 `flag.Bool` 函数定义 `-d` 选项 3. 调用 `flag.Parse()` 函数解析命令行选项 4. 在程序中使用 `*d` 来判断是否有 `-d` 选项 ### 回答2: 在golang中,可以使用flag包来解析命令行参数。首先,我们需要定义一个命令行参数变量来接收用户输入的-d选项。然后,调用flag.BoolVar函数将命令行参数与该变量关联起来。接着,我们可以使用flag.Parse函数来解析命令行参数。 下面是一个示例代码: ```go package main import ( "flag" "fmt" "os" "os/signal" "syscall" ) var isDaemon bool func main() { flag.BoolVar(&isDaemon, "d", false, "启用后台执行模式") flag.Parse() if isDaemon { // 启用后台模式 fmt.Println("进入后台执行模式") go daemonize() // 在后台执行模式下,程序不会终止 // 可以在这里添加你的后台任务代码 // 消息通知,防止程序立即退出 sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) <-sigChan return } // 在非后台模式下,继续执行其他命令行操作 fmt.Println("执行用户命令行操作") } // 后台执行代码 func daemonize() { // 添加后台执行逻辑 // 例如,模拟后台任务执行 for i := 0; i < 10; i++ { fmt.Println("后台任务执行中...") } } ``` 在上述代码中,首先定义了一个布尔类型的全局变量isDaemon用于接收-d选项是否存在,默认值为false。然后使用flag.BoolVar函数将-d选项与该变量关联起来,设置了-d选项的默认值为false,并添加了相关的说明信息。接下来调用flag.Parse函数来解析命令行参数。 在main函数中,首先判断isDaemon的值,如果为true,则表示用户输入了-d选项,程序将进入后台执行模式。在后台执行模式下,你可以添加你的后台任务代码,例如打开socket、启动服务等。 为了防止程序立即退出,我们可以使用signal包来监听中断信号,例如Ctrl+C和SIGTERM。当接收到中断信号时,程序将退出。 如果isDaemon的值为false,则表示用户没有输入-d选项,程序将继续执行其他命令行操作,例如执行用户的命令。 以上代码演示了如何使用golang来设置程序判断是否有-d选项并在有-d选项时进入后台执行不终止程序,不阻塞用户命令行操作。 ### 回答3: 在Golang中,可以通过使用标准库中的"flag"包来解析命令行参数。为了实现程序判断是否有"-d"选项并且启动后进入后台执行且不终止程序,可以按照以下步骤进行操作: 1. 首先,需要导入"flag"包和其它可能需要使用的包: ```go import ( "flag" "os" "os/signal" "syscall" ) ``` 2. 接下来,在main函数中定义一个布尔类型的变量,用来表示是否有"-d"选项: ```go var inBackground bool ``` 3. 然后,使用flag包来定义命令行参数,并将其与上一步中定义的变量绑定: ```go func init() { flag.BoolVar(&inBackground, "d", false, "后台执行,不终止程序") flag.Parse() } ``` 4. 接下来,将程序进入后台执行的逻辑写在一个单独的goroutine中,并在程序退出时进行清理: ```go func runInBackground() { // 进入后台执行的逻辑 // 处理程序退出的信号 signals := make(chan os.Signal, 1) signal.Notify(signals, os.Interrupt, syscall.SIGTERM) <-signals // 执行清理逻辑 } func main() { if inBackground { go runInBackground() } else { // 其它正常执行的逻辑 } select{} } ``` 通过上述步骤我们可以实现在程序启动时判断是否有"-d"选项,如果有则进入后台执行,并且不终止程序,不会阻塞用户在命令行上的操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值