Spearfisher
Spearfisher

Reputation: 8783

use custom Go packages

I have written some custom packages which I am struggling to reuse.

My code is structured as follows:

$GOPATH/src/github.com/myusername/myfirstrepo/

In myfirstrepo I have 2 different packages:

myfirstrepo
   |--somefolder1/package1
   |--somefolder2/package2

Now I'd like to import my package1 in package2, I know I can use my published code on github using go get but I'd like to know if it's possible to use something similar to npm:

import "./../somefolder1/package1"

Obviously I've tried this and it doesn't work.

How can I simply do this?

Upvotes: 2

Views: 79

Answers (1)

VonC
VonC

Reputation: 1328522

Now I'd like to import my package1 in package2, I know I can use my published code on github using go get

It doesn't have to be published on github.
The import path just has to be a valid path within your workspace ($GOPATH)

import "github.com/myusername/myfirstrepo/somefolder1/package1"

Relative paths are not the best practice (unless you really need to access your functions without classifier).

Upvotes: 2

Related Questions