0

I have this directory layout:

/baba
   biz.go         # package baba
/hello
   foo.go         # package main

biz.go looks like this:

package baba

func Foodd(z int) int {
    return z + 5
}

and foo.go looks like this:

package main

import (
    "fmt"
    "log"
)

func main() {
    log.Fatal(Foodd(3))
}

currently this doesn't compile because Foodd is not recognized. How do I import the baba package in foo.go?

I assume if I compile like so, that it will pull in the right files:

go build foo.go

Or do I need to include the files in the baba package in the go build command? (I would hope not).

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • You seem to be having a number of issues all covered in the docs. Have you gone through the intro material, like [How To Write Go Code](https://golang.org/doc/code.html)? – JimB Apr 10 '18 at 21:03
  • yeah, importing code is tricky here, as is usual when learning new languages, I am just trying to figure it out – Alexander Mills Apr 10 '18 at 21:08
  • @JimB for example the baba directory and the hello directory are at `$GOPATH/src/hello` and `$GOPATH/src/baba`, so why is this hard? – Alexander Mills Apr 10 '18 at 21:09
  • I am guessing I need to do something like this `$GOPATH/src/myusername/hello` and `$GOPATH/src/myusername/baba` – Alexander Mills Apr 10 '18 at 21:10
  • There's a step by step example in the docs I linked. You're neither importing `baba`, nor are you qualifying the `baba.Fodd` identifier with the package name. – JimB Apr 10 '18 at 21:15
  • Got it thank you. I wish golang would enforce packages to match their directory names. In fact I see no reason why the package declaration exists at all. Oh well. The package name could just be the name of the directory, and we could forgo the package declaration. I wonder what the reasoning is. – Alexander Mills Apr 10 '18 at 21:19

1 Answers1

1

You need to import the baba package in order to use it from your main package. That will look something like:

package main

import (
    "fmt"
    "log"

    "github.com/the1mills/myproject/baba"
)

func main() {
    log.Fatal(baba.Foodd(3))
}

Imported packages are referred to by their package name, which is usually, but not always, the last element of the import path.

Usually, people let goimports handle finding the correct import path and automatically adding it. Your editor of choice probably has goimports integration.

Also see this answer for some other references and how to set up your directory structure.

Tyler B
  • 1,618
  • 9
  • 20
  • yes but what if the package is just local, for example, let's say the package is at `$GOPATH/src/baba` – Alexander Mills Apr 10 '18 at 21:06
  • 2
    You should not create root-level packages, even if they are just local. You can create a directory structure under a namespace of your choosing, though. See https://golang.org/doc/code.html#ImportPaths. (For the record, it will work if you create a root-level package, but it will be difficult to maintain and will probably break. You don't have to push your code to GitHub to be able to import it locally.) – Tyler B Apr 10 '18 at 21:17