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

前端 未结 11 2516
滥情空心
滥情空心 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:31

    Finally we can now use:

    go run .
    

    thanks to: https://github.com/golang/go/issues/22726#issuecomment-345841019

    0 讨论(0)
  • 2020-12-08 02:36

    On Windows I usually just add a little test.bat to all my project directories:

    go build
    .\{project_name}.exe
    go clean
    

    Works well enough. Replace {project_name} with the name of your executable, of course. And once the executable finishes, the batch script moves on to the clean up.

    0 讨论(0)
  • 2020-12-08 02:36

    For window the following works: Open cmd and go to the path where your folder exists. Then type the following command and press Enter.

    go build
    

    after this one executable will be created. Then in the command prompt call the executable. If your executable name is Project.exe then type the following and press Enter:

    Project.exe
    
    0 讨论(0)
  • 2020-12-08 02:37

    If i understand your question right, you need import other code as libraries.

    Little example

    ./testing/main.go:

    package main
    
    import "fmt"
    import L "testing/lib"
    
    func main() {
        fmt.Println("Hello from main()")
        L.Somefunc()
    }
    

    ./testing/lib/somelib.go:

    package lib
    
    import "fmt"
    
    func Somefunc() {
        fmt.Println("Hello from Somefunc()")
        return
    }
    

    To launch - go run main.go

    0 讨论(0)
  • 2020-12-08 02:38

    You can run all .go files, excluding tests, using this bash construction:

    go run $(ls -1 *.go | grep -v _test.go)
    
    0 讨论(0)
提交回复
热议问题