1

I am new to go lang and I m trying out my first app. I hit a roadblock while importing a package from another go file.

I have two go files. 1. hello.go with main package. 2. data.go with data package.

The code inside hello.go is

   package main

   import (
    "datatypes/data"
    "fmt"
       )

  func main() {
      fmt.Print("Hello world!!\n")
      /* calling the first function from another function */
      myFirstFunction()
   }

  func myFirstFunction() {
      fmt.Print("This is the first function\n")
      data.WorkingWithDataTypes()
  }

data.go code is

   package data

  import "fmt"

  /*WorkingWithDataTypes is a function to work with go lang data types */
  func WorkingWithDataTypes() {
    x := 10
    fmt.Print(x)
   }

When I run the main function by running go run hello.go I am getting the below error

**hello.go:10:2: cannot find package "firstapp/data" in any of:     
/usr/local/go/src/firstapp/data (from $GOROOT)  
/Users/home/workspaces/Go/Project1/src/firstapp/data (from $GOPATH)**

I set my go path as /Users/home/workspaces/Go/Project1. I am not sure where I make the mistake. any help?

I set the GO PATH as

enter image description here

As per the go doc for go path

I created the project structure as below and put my code below src folder.

src/datatypes has the package data. src/main has the package main.

But still go run hello.go is failing as it is not able to find the package datatypes/data

enter image description here

GAK
  • 1,018
  • 1
  • 14
  • 33
  • 1
    Packages need to be a directory. See [How to Write Go Code](https://golang.org/doc/code.html). Start off with a single `GOPATH`, you can even use the default of `$HOME/go` and not worry about it at all. – JimB Sep 11 '17 at 13:02
  • You should place your project `firstapp` under `/usr/local/go/src` for it is where your `GOPATH` points to. Moreover you can't have 2 different packages in the same directory. So `data.go` should be in package `main`too. – TDk Sep 11 '17 at 13:02
  • @TheophileDano: no, that is `GOROOT`, and you should _never_ put your code in `GOROOT`. – JimB Sep 11 '17 at 13:03
  • @JimB before he edited the post I thought I had seen gopath set to this location... – TDk Sep 11 '17 at 13:05
  • 1
    @TheophileDano: `/usr/local/go/` is the default Go install path -- it should never be GOPATH. (P.S., can view the edit history of the post) – JimB Sep 11 '17 at 13:08
  • @volker I edited my question. It is unique as I believe i follow the documents but the code is still breaking. – GAK Sep 11 '17 at 14:22
  • @jimB and @ TheophileDano i modified the structure and set the GOPATH to point to my project1 directory but still unable to find the package. – GAK Sep 11 '17 at 14:23
  • As I said in the first comment, a package must be a directory. You don't have an import path of `datatypes/data` because there is no `data` directory. – JimB Sep 11 '17 at 14:26
  • @JimB thanks I changed my package declaration as datatypes instead of datatypes/data in the hello.go and it fixed it. Thanks for the help. – GAK Sep 11 '17 at 14:31

0 Answers0