3

First of all and to be clear I come from the Java world, and I have been programming on go for a while and I really love it.

I have a small question about the packaging system and the imports, if I am importing a library which uses another library and I am already using that library in my project, how can I eliminate the duplication(if its possible),

in other words: A is a the main program, C and B are libraries, and then: C was added to A B uses C

and then B was also added to A

AProject/
    src/
        LibC/
            src/
                somefiles.go
        LibB/
            src/
                LibC
                somefiles.go
        app.go

So now I have two libraries of C one in A since the beginning and one in B because B is dependent on C.

I know its a little bit confusing but in the Java world we have Ant and Maven and those build tools make it really easy to us to handle the dependencies.

Any thoughts?

2 Answers 2

9

In Go, there is no duplication of packages.

First, you should read about Go workspaces in How to Write Go Code.

From your question, your directory structure should look something like this:

gopath (gopath is the path of a directory in your $GOPATH list)
├── bin
│   └── projecta
├── pkg
│   └── linux_amd64
│       └── projecta
│           ├── libb.a
│           └── libc.a
└── src
    └── projecta
        ├── a.go
        ├── libb
        │   └── b.go
        └── libc
            └── c.go

Where,

gopath/src/projecta/a.go:

package main

import (
    "projecta/libb"
    "projecta/libc"
)

func a() {
    libb.B()
    libc.C()
}

func main() { a() }

gopath/src/projecta/libb/b.go:

package libb

import (
    "projecta/libc"
)

func B() { libc.C() }

gopath/src/projecta/libc/c.go:

package libc

func C() {}
Sign up to request clarification or add additional context in comments.

3 Comments

Trust me I know the structure but imagine that I didn't use LibB okay and I am using LibC, and everything is fine, and then I had a new use case for my project where I need to go get LibB, and LibB(which is a public Library on git hub let us say) it uses LibC implicitly because of some functionality that LibC provides ok, no I want to use LibB and LibC in my project, Now I have to possibilities: 1- remove LibC and use it indirectly from LibB. 2-keep it and then I will have a libs duplication, Is that correct ?
The imported package (LibC in this example) will only appear once in your compiled Go executable, and once in your $GOPATH/src/github.com/LibC archive. You may import that 3rd party package in any number of implementation modules, only one instance of the compiled object will be added to your final executable Go program.
Thank you This is all what I needed to hear, this little notice, that will appears once, the thing is I don't feel comfortable with the code seeing all that resource files as libs I am used to jars(because I come from a Java background) that's why it was odd for me a little bit, but yeah this is how things work in go and its fair enough
6

If you are talking about third-party libraries, in go is very simple to do that, just put the import in your source code, like:

import "github.com/somepackage/somelib"

and from the command line in your working directory run:

go get

the the source code of the libraries will be downloaded in the src directory of your $GOPATH. If you want to create your own lib instead, just create the folder named as the lib in $GOPATH/src and put the code in this folder.

The folders structure is:

$GOPATH/
src/
    github.com/
        somepackage/
            somelib/
                somelib.go
    yourlibB/
        yourlibB.go -> //import somelib here
    yourlibC/
        yourlibC.go -> //import libB here
    yourmainprogramA/
        yourmainprogramA.go -> //import somelib, libC and libB here

6 Comments

but this is not what I need, I know how to structure your code but what I am saying is how to remove a duplication of a library, not how to fetch that library, and I don't need to run go git because I am using IntelliJ Idea and I can fetch any library automatically
Sorry i don't get of what duplication are you talking about, when you build if you require a lib it will be included once, even if it's included in every sources and libs
okay look at your example, yourlibB is a package that uses libc, so forget about the project for a moment and look only at libB so in Lib B I will have this structure: libB/ src/ gofiles.go , libC Project A -> src-> libC,lbB,somgofiles.go This is the structure
your example is wrong, src must be the root of all, imported libs are placed all in your GOPATH/src folder. In your example paths libB and libC are assumed as separated GOPATHs, and not GOPATH/src/libB and GOPATH/src/libC
@user2422033 if you insist on using non-standard (and broken) workspace layouts then you're on your own and you shouldn't expect help from the standard tools. In Go unique packages should have a unique location and a unique import path and then everything works as intended.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.