避免在“Go run”-ng时输入主包中的所有go文件 [英] Avoid typing out all go files in the main package when "Go run"-ng

查看:107
本文介绍了避免在“Go run”-ng时输入主包中的所有go文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main 包中有多个.go文件时,我需要在执行 go run时将它们全部列出



所以当我有 main.go a.go b.go ,它们都属于主包,我需要键入 go run main.go a.go b.go 以便在其他2个go文件中使用函数和结构。然而, go build 命令足够聪明,可以自动将所有文件链接在一起。



我误解了Go ,或者这是正常的(在执行 go run 时,列出主要包中的所有文件)解决方案

简单的答案是:您需要将它们全部列出。如果你绝望,你可以用shell技巧做到这一点。我通常只是编写一个shell脚本到 go build ,然后在得到的可执行文件变得太烦人后运行。



<$除非你有_test.go文件
去运行* .go

#如果你真的想要...启用扩展glob
shopt -s extglob
#所以你可以做这样的事情(匹配除* _test.go文件以外的所有.go)
去运行!(* _test).go

查看此主题: https://groups.google.com/forum/#!topic/golang-nuts/EOi0VAQNA4c


When you have multiple .go files in a main package, i need to list them all out when doing a go run.

So when i have main.go, a.go, b.go and they all belong to the main package, i need to type go run main.go a.go b.go in order to use functions and structs inside the other 2 go files. The go build command however, is smart enough to link all files together automatically.

Have i misunderstood something about Go, or is this normal (listing out all the files in the main package when doing a go run)?

解决方案

The short answer is: you need to list them all out. You can do this with shell tricks, if you're desperate. I usually just write a shell script to go build and then run the resulting executable, after it gets too annoying.

# this works unless you have _test.go files
go run *.go

# if you really want to... enable extended glob
shopt -s extglob
# so you can do something like this (matches all .go except *_test.go files)
go run !(*_test).go

Check out this thread: https://groups.google.com/forum/#!topic/golang-nuts/EOi0VAQNA4c

这篇关于避免在“Go run”-ng时输入主包中的所有go文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆