Can I have a library and binary with the same name?

前端 未结 1 1633
误落风尘
误落风尘 2020-12-15 07:45

I\'m building a library, but I also want it to be usable as a standalone binary.

For example, let\'s say I\'m building an implementation of Tar. Tar is commonly used

相关标签:
1条回答
  • 2020-12-15 07:59

    I'd probably do this

    src/
        tar/
            tar.go         # tar libary
            tar/
                main.go    # tar binary
    

    That will give you a binary called tar and a library called tar

    Let's say you are hosting this on github then you'd want

    src/
        github.com/
            you/
                tar/
                    tar.go         # tar libary
                    tar/
                        main.go    # tar binary
    

    Which would give you a binary called tar when you do go get install github.com/you/tar/tar and a library called github.com/you/tar when you do go get install github.com/you/tar

    Depending on which you feel is more important you could swap the library and the binary over

    src/
        github.com/
            you/
                tar/
                    main.go            # tar binary
                    tar/
                        tar.go         # tar libary
    

    Keeping all the code in one tree enables you to do go install ./... from the root to build all packages and subpackages which is an advantage. go test|fmt ./... also. (Note that really is 3 dots!)

    0 讨论(0)
提交回复
热议问题