0

I checked out this answer and for some reason either I was unable to comprehend properly or it didn't work

Also, Before I start, I got that How we can do it with github but I want to try it without github

To start let's say I have a main.go file

package main

import (
    "fmt"
    "math"
    "subpack"
)

//You Import packages without using comma in Go, rather space or new line
//In VS Code, if you use aren't using the package and run then it will automatically removie it

func main() {
    fmt.Println("hello world")
    //We use math.Floor to round the nunmber
    fmt.Println(math.Floor(2.7))
    fmt.Println(math.Sqrt(16))
    fmt.Println(subpack.Reverse)
}

Notice subpack here, it is the custom package I made. The subpack exsist like this

enter image description here

And contains the following code

package subpack

//If we make this in the same root level of our main it will throw an error

func Reverse(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

running our go is throwing following error

cannot find package "subpack" in any of:
        /usr/local/go/src/subpack (from $GOROOT)
        /Users/anilbhatia/go/src/subpack (from $GOPATH)

Question: Is it possible and if yes, How to use custom package without github and without making it in the GO main folder rather by simply referencing the folder containing our go file from the directory we are working.

Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
  • 3
    Go doesn't really have a concept of "subpackages". In order import a package, you need to reference the fully qualified import path. So for example, if your main package has an import path of `github.com/me/mycoolprogram`, you would do `import "github.com/me/mycoolprogram/subpack"` in your `main.go`. – Joel Cornett Dec 21 '18 at 05:58
  • 1
    @JoelCornett since even you happen to work on Javascript, For a person coming with a background of Js and is habitual of creating helper functions (specific for a project) and then importing it and using them. How can they have a way around? – Alwaysblue Dec 21 '18 at 06:02
  • 1
    Also, I don’t want my code to be on GitHub. – Alwaysblue Dec 21 '18 at 06:03
  • 2
    There is no "I want" and "how to circumvent the proper way". You do not need to upload you code to github but you **must** layout you code in GOPATH as described in How to Write Go Code. There is nothing to change here. – Volker Dec 21 '18 at 07:57
  • 2
    u need to put your project in `$GOPATH/src`..then `import "yourprojectname/subpack"` – Donald Wu Dec 21 '18 at 08:27

3 Answers3

2

As the error shows, the compiler cannot find subpack from either

/usr/local/go/src/subpack (from $GOROOT)

where the standard library packages (such as fmt, strings) are, or

/Users/anilbhatia/go/src/subpack (from $GOPATH)

where user installed/defined packages are.

To make the import work, you just need to include the relative path of the subpack package (relative to $GOPATH/src) in your main.go

Suppose your main.go is in /Users/anilbhatia/go/src/parentpack, then its import should be

import "parentpack/subpack"

If I understand you correctly, you want the caller of subpack (say main.go) to be in an unrelated location of subpack. This actually works out of box. Your main.go could be located anywhere. When you compile it, the compiler sees the import path of parentpack/subpack, and goes to $GOPATH/src and $GOROOT/src to find it.

For more information about the source code organization and some typical examples, you can run

 go help gopath

in your shell.

nos
  • 19,875
  • 27
  • 98
  • 134
  • 1
    Can you please do that same and update in your answer? – Alwaysblue Dec 21 '18 at 06:09
  • 1
    where is your `main.go` located? – nos Dec 21 '18 at 06:11
  • 1
    then how will I perform I execute my `fmt.Println(subpack.Reverse("yollo"))` ? here subpack was earlier coming from `import "subpack"` Also my `main.go` is at `Macintosh HD⁩ ▸ ⁨Users⁩ ▸ ⁨anilbhatia⁩ ▸ ⁨Desktop⁩ ▸ ⁨go⁩ ▸ ⁨packages⁩` – Alwaysblue Dec 21 '18 at 06:16
  • 1
    You need to do some file organization. Your packages should be located in `$GOPATH/src`, that's how the compiler finds where the importable libraries are. So you can either move your packages to the current `$GOPATH/src`, or add more path for `$GOPATH`. – nos Dec 21 '18 at 06:22
1

The starting point is $GOPATH/src not your project's folder.
So you should use

import "myproject/subpack"

instead of :

import "subpack"
Mostafa Solati
  • 1,235
  • 2
  • 13
  • 33
0

For some reason all the answers to this question are more than 2 years old and outdated. You can definitely do that now. I think there was a change in a recent version of Go that allows importing custom packages that are not in $GOPATH. You can do what you want to do simply by importing your "subpackage" like this in the main.go:

import "./subpack"
Sebastian Dengler
  • 1,258
  • 13
  • 30