简体   繁体   中英

how to organize GO project packages if it will hosted on different repos (GitHub & SourceForge)

Say I want to create a project and host it on GitHub,I HAVE TO create project struct like this:

src/github.com/user/      
     myproject/
     main.go
     util/
       fileutil.go

and in the main.go ,I have to write the import as:

import github.com/user/myproject/util/fileutil

AND now I also want to host this project onto SourceForge, should I copy the whole project and modify the path ?? it seems not so good enough. Is there any other way to do that ? What I need is just create my project under src folder, and can be hosted to any repo as I wish, without changing the packages.

If developing an app and not a library

If you don't plan to have other projects "import" your own project (this is, if you are implementing an application and not a library), it might be possible for you to avoid any reference to github.com in your local paths.

Note: this is not the recommended approach, but from my tests it does work, you can compile, run and test your code if you structure it this way.

You can create your project structure as follows:

src/
    myproject/
        main.go
        util/
            fileutil.go

Then you can import like this:

import "myproject/util/fileutil"

You can then host the contents of the myproject folder anywhere without changing paths within the project files.

If developing a library

If you are actually developing a library others will be able to import, then it gets more complicated since those projects will actually need a full path to import your project.

You can create a "vanity" import path, like myproject.io/... by using the meta tag like described here:

https://golang.org/cmd/go/#hdr-Remote_import_paths

That way, when the go tool queries your myproject.io/ pages, you should respond with a header like this:

<meta name="go-import" content="myproject.io git https://github.com/user/myproject">

And then change that header you return if you decide to move away from github.

Note that this does not prevent users to import your project directly from github, if you want to avoid that you need to use the technique described in the canonical import path spec:

https://golang.org/doc/go1.4#canonicalimports

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM