Home >Backend Development >Golang >Can I Give My Go Library and Binary the Same Name?

Can I Give My Go Library and Binary the Same Name?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-01 17:41:13502browse

Can I Give My Go Library and Binary the Same Name?

Can I Create a Library and Binary with the Identical Name?

Query:

A library and binary are being constructed, with the intention of having the binary usable independently. However, attempts to accomplish this result in a command named differently from the intended, and a discovered workaround doesn't feel optimal. Is there a more appropriate approach?

Response:

Optimized Directory Structure for Coexisting Library and Binary:

A recommended approach is to structure the directory as follows:

src/
    tar/
        tar.go         # Tar library
        tar/
            main.go    # Tar binary

This configuration yields a binary named tar and a library named tar.

GitHub Repository Considerations:

If hosting the library and binary on a platform such as GitHub, the following directory structure is recommended:

src/
    github.com/
        you/
            tar/
                tar.go         # Tar library
                tar/
                    main.go    # Tar binary

This arrangement results in the binary tar upon execution of go get install github.com/you/tar/tar and the library github.com/you/tar upon execution of go get install github.com/you/tar.

Alternative Arrangements:

The library and binary positions can be reversed depending on priority:

src/
    github.com/
        you/
            tar/
                main.go            # Tar binary
                tar/
                    tar.go         # Tar library

This alternative structure enables convenient execution of commands like go install ./... from the root directory to build packages and subpackages.

The above is the detailed content of Can I Give My Go Library and Binary the Same Name?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn