golang using functions of imported subdirectories

13,425

Your approach is completely wrong. Go has absolutely no concept of importing files or directories, all you can import in Go are packages. It now happens that the name of a package is it's path relative to GOPATH and you import packages by that name. But the identifier under which an imported package is available in the importing code depends on the package declaration of the package. You cannot simply "move" files between directories as each directory (for the go tool) is a single package without changing the package declaration.

You can have package x under path a/b/c. When you import package x with import ( "a/b/c" ) all the exported stuff from package x is available to you as x.ExportedName.

Please read http://blog.golang.org/organizing-go-code carefully.

Share:
13,425
Admin
Author by

Admin

Updated on June 16, 2022

Comments

  • Admin
    Admin almost 2 years

    I can't use functions of custom subdirectories.

    My Code Organziation

    I have under "src" a path hierarchy like

    a/b

    with all my directories and go-Files (it is the "root" of my project). The directories contain no subdirectories and it works fine. So the deepest path is "a/b/c". E.g. I have

    a/b/c

    and

    a/b/d

    with some go-files. Import of "a/b/d" and calling a function with "d.DoSomething()" from a file in "a/b/c" works fine.

    Problem description

    Now I want ot reorganize "a/b/d". I move some files from "a/b/d" to

    a/b/d/e

    and the rest of the files to

    a/b/d/f

    If try to import "a/b/d/e" with import-statement

    import ( "a/b/d/e" )

    from the same file in "/a/b/c" and want to call "e.DoSomething()" (it is the place, where the file with the "DoSomething-function" moved to), I get an error at the line, where I call "e.DoSomething()": "undefined: e".

    While searching for a result, I've nowhere seen examples with deeper path hierarchies. Is it generally not possible to use/import subdirectories or what's the problem?

    go-version I used: go1.2.2 linux/amd64


    Thanks for any advices