7

I'm trying to make a simple calculator in Go. I'm designing it in such a way that I can build a command-line interface first and easily swap in a GUI interface. The project location is $GOPATH/src/gocalc (all paths hereafter are relative to the project location). The command-line interface logic is stored in a file gocalc.go. The calculator logic is stored in files calcfns/calcfns.go and operations/operations.go. All files have package names identical to their filename (sans extension) except the main program, gocalc.go, which is in the package main

calcfns.go imports operations.go via import "gocalc/operations"; gocalc.go imports calcfns.go via import "gocalc/calcfns"

To summarize:

  • $GOPATH/src/gocalc/
    • gocalc.go
      • package main
      • import "gocalc/calcfns"
    • calcfns/
      • calcfns.go
        • package calcfns
        • import "gocalc/operations"
    • operations/
      • operations.go
        • package operations

When I try to go build operations (from the project dir), I get the response: can't load package: package operations: import "operations": cannot find package When I try go build gocalc/operations, I get can't load package: package gocalc/operations: import "gocalc/operations": cannot find package When I try go build operations/operations.go, it compiles fine

When I try to go build calcfns or go build gocalc/calcfns, I get can't load package... messages, similar to those in operations; however, when I try to build calcfns/calcfns.go it chokes on the import statement: import "gocalc/operations": cannot find package

Finally, when I try go build . from the project dir, it chokes similar to the previous paragraph: import "gocalc/calcfns": cannot find package

How should I structure my child packages and/or import statements in such a way that go build won't fail?

TylerH
  • 20,799
  • 66
  • 75
  • 101
weberc2
  • 7,423
  • 4
  • 41
  • 57

3 Answers3

7

Stupidly, I forgot to export my GOPATH variable, so go env displayed "" for GOPATH. (thanks to jnml for suggesting to print go env; +1).

After doing this (and moving the main program to its own folder {project-dir}/gocalc/gocalc.go), I could build/install the program via go install gocalc/gocalc.

Moral of the story, make sure you type export GOPATH=... instead of just GOPATH=... when setting your $GOPATH environment variable

weberc2
  • 7,423
  • 4
  • 41
  • 57
1

Please try to also add output of $ go env to provide more clues. Otherwise both the directories structure and (the shown) import statements looks OK.

However the sentence

When I try to go build operations (from the project dir), I get the response: can't load package: package operations: import "operations": cannot find package

sounds strange. It seems to suggest you have

package operations

import "operations"

in 'operations.go', which would be the culprit then...?

zzzz
  • 87,403
  • 16
  • 175
  • 139
  • No, `operations` wasn't trying to import itself. ;) But your `go env` did show me that I used `GOPATH=$HOME/Projects/Go` instead of `export GOPATH=$HOME/Projects/Go`, which was the real culprit. Interestingly, `echo $GOPATH` still produced "`[abs path to ~]/Projects/Go`"; however, `go env` showed `GOPATH=""` or some such. Thanks for the suggestion. +1 – weberc2 Jan 19 '13 at 18:49
-1

Very easy:

Lets say I have a project/app named: golang-playground

  1. Put your root dir under GOPATH/src/, in my case GOPATH=~/go/src (run command go env to get your GOPATH). so complete path for my app is ~/go/src/golang-playground enter image description here

  2. Lets say you want to use function Index() inside of file: router.go from my main.go file (which of course is on root dir). so in main.go:

    import (
        ...
        "golang-playground/router"
    )
    
    func main() {
        foo.Bar("/", router.Index) // Notice caps means its public outside of file
    }
    
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186