-1

I'm learning Go and I've made this example: http://thenewstack.io/make-a-restful-json-api-go/ to build a simple REST API.

I've compiled it and all works fine but all sources are in the main package.

Now I want to organize my .go files in packages, so I move them into some folders this way:

GOPATH\bin
GOPATH\pkg
GOPATH\src\pack1\Handlers.go
GOPATH\src\pack1\Logger.go
GOPATH\src\pack1\repo.go
GOPATH\src\pack1\Todo.go
GOPATH\src\router\Router.go
GOPATH\src\router\Routes.go
GOPATH\src\Main.go

Main.go uses all the router package so I've put in the import section: "./router". Router.go uses the pack1 package, so in Router.go I've imported "../pack1". Now if I try to "go build Main.go" I get:

router\Router.go:6: imported and not used: "_/D_/GOPATH/src/pack1"
router\Router.go:14: undefined: Logger

and similar errors, so seems that the import of pack1 package that I've made, it's wrong. Of course in all files belonging to pack1, in the header I've put the "package pack1" definition.

I've also read that the relative imports are not suggested in Go and it could be useful to use the remote packages such ad importing "github.com/myrepo/mypackage". But I dont want to use remote imports; I want to push all my files in a second moment.

Could you help me to better understand what is the way to have local imports between packages in Go?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Roberto Milani
  • 760
  • 4
  • 20
  • 40
  • 2
    Read (or re-read) through [How to Write Go Code](https://golang.org/doc/code.html) which will explain everything in more detail. – JimB May 19 '16 at 00:27
  • 1
    Please note that the errors read "imported and not used" so the import kinda worked but you did not use any symbol of package pack1. This relates to the next error "undefined: Logger": If `Logger` lives in package pack1 than you reference it from any other package as `pack1.Logger`. Re-read "How to Write Go Code" and take a look at some working packages online. – Volker May 19 '16 at 06:01
  • Thanks guys for the answer, now I've understood how to import packages in the right way! This way it's also easier! I have one last question: I still can't compile the project: inside Router.go there's an instance of Logger so in Router.go I've imported the package "pack1" but it says that Logger is undefined :\ where am I doing wrong? – Roberto Milani May 19 '16 at 20:06
  • @RobertoMilani: please show an example of your code that causes the error. You either need to qualify the identifier with the package name, or you're not defining things where you think you are. – JimB May 19 '16 at 20:18
  • Sure! This is Router.go in package "router" (http://pastebin.com/P9uTRedF) and this is Logger in package "pack1" (http://pastebin.com/STWRMZFs). Thanks m8 – Roberto Milani May 19 '16 at 20:30
  • Problem solved. I forget to put the package name before the classname. Thanks for all man! – Roberto Milani May 20 '16 at 07:39

2 Answers2

4

The reason you're not supposed to use relative imports is that they don't work inside of $GOPATH. Get rid of the "relative" part of your import paths, since all imports are relative to $GOPATH/src

import (
    "pack1"
    "router"
)

You will also want to move your Main.go into a package as well, so you you can make full use of the go tools.

JimB
  • 104,193
  • 13
  • 262
  • 255
  • Thanks man! Now I have understood and all my imports are ok! :) And if from Router.go I call a function that is defined in a .go file inside pack1, what can I do? I've imported pack1 from Router.go but when I try to "go install" it says that the function is undefined... :\ Thanks in advance – Roberto Milani May 19 '16 at 20:18
  • Problem solved, the package name was missing before when I use a class from a different package. – Roberto Milani May 20 '16 at 07:40
0

As JimB says, read through the 'How to write go code' link, specifically https://golang.org/doc/code.html#ImportPaths

  • All packages exist inside a directory tree starting at $GOPATH/src.
  • Import path is effectively the full path to the package (from 1)
  • There are no sub packages in GO.
  • main packages are used to create commands

So in your case, your import paths will be "pack1" and "router"

factotum
  • 900
  • 10
  • 13