0

I am trying to write a program where I have a structure like:

Go/src/
  -github.com
     -myname
        -hello
           main.go
           -vector
              vector.go

When I import the package in my code inside of the main.go file using a command like:

import(
  "vector"
)

I get the error message:

Can't find package "vector" in any of:
    C:\Go\src\vendor\vector (vendor tree)
    C:\Go\src\vertex (from $GOROOT)
    C:\Go\src\github.com\myname\src\vertex (from $GOPATH)

Why is it adding src on that last line? Shouldn't it replace the src with the hello folder since that is where I'm running the file from? Also, it runs if I import it from the full file structure like github.com/myname/hello/vertex which seems strange to me.

I am executing using go run hello.go to simplify my interaction with the program.

John Weldon
  • 39,849
  • 11
  • 94
  • 127
Rob
  • 3,333
  • 5
  • 28
  • 71

1 Answers1

3

Your Go code (as opposed to Go's stdlib) is meant to be under $GOPATH/src (edit: not $GOROOT, as I initially said!), and it's standard to always use the full import path, in your case starting with github.com/ (even if you figured out a way to avoid having to).

There is more in How to Write Go Code by the Go team, and other answers here describing project layout and the first steps to setting a workspace up.

Community
  • 1
  • 1
twotwotwo
  • 28,310
  • 8
  • 69
  • 56
  • So if I understand correctly, my personal code goes in the `$GOROOT/src`, but if I import a repo from someone else's code, I would put that in the `$GOROOT/src/github.com/username/reponame` folder? – Rob Apr 05 '16 at 18:37
  • 1
    A user's code is supposed to be under $GOPATH[/src], not $GOROOT[/src]. – cd1 Apr 05 '16 at 20:00
  • @cd1 ugh, very sorry, fixed – twotwotwo Apr 05 '16 at 20:38
  • @Rob -- see above; I was dead wrong. Generally, your code and other people's code should be in a similar structure -- see "the first steps..." link for an example where you make `$GOPATH/src/github.com/youruser" – twotwotwo Apr 05 '16 at 20:39