Does it make sense to have two packages in the same directory?

前端 未结 4 544
没有蜡笔的小新
没有蜡笔的小新 2020-12-16 09:43

I have a project that provides a library (exports some funcs) and also must provide a command-line interface (there must be an executable file).

Example of directory

4条回答
  •  眼角桃花
    2020-12-16 10:10

    You can't have two golang files in one directory with two packages. So you need to move main.go out of myproject.

    the directory structure before move

    whatever.io/
        go.mod
        myproject/
            main.go
            myproject.go
    

    After move

    whatever.io/
        go.mod
        main.go
        myproject/
            myproject.go
    

    And you need to change your main.go's import path. If the module name is aaa

    Before

    import "aaa"
    

    Need change to this

    import "aaa/myproject"
    

提交回复
热议问题