How to run all .go files within current directory through the command line (multi file package)

前端 未结 11 2548
滥情空心
滥情空心 2020-12-08 01:49

I\'m a newcomer to Go. I extremely like the language, but I quickly realised that I needed to start dividing my files due to an increase in program size.

go r

11条回答
  •  一生所求
    2020-12-08 02:24

    As Nate Finch notes:

    Go run is ... really only meant to be used on very small programs, which generally only need a single file.

    Even on unix, go run *.go is often not correct. In any project with unit tests (and every project should have unit tests), this will give the error:

    go run: cannot run *_test.go files (something_test.go)
    

    It will also ignore build restrictions, so _windows.go files will be compiled (or attempted to be compiled) on Unix, which is not what you want.

    There has been a bit of discussion of making go run work like the rest of the go commands, and there's an open CL for it (5164). It's currently under consideration for Go 1.4. In the meantime, the recommended solution on all platforms is:

    go build && ./
    

提交回复
热议问题