-1

Ok so this question has been asked before and I believe I have looked through all of them to and tested the answers but I will explain how each one doesn't match my case. I might have missed the answer in one of those but I read each one and attempted to see if it could fit my case

How to import local packages in go? I believe my imports follow the structure of the answer

Go build: "Cannot find package" (even though GOPATH is set) I'm not sure if this one is fully pertinent but I don't think it's the same error.

Golang - Why can't I import local package in GOPATH/src/project but can in home directory? My import path isn't relative so this question isn't pertinent.

My error is simple:

cannot find package "api/handlers" in any of: 
C:\Go\src\api\handlers (from $GOROOT) 
C:\Projects\Go\src\api\handlers (from $GOPATH)`

My project structure is as follows:

src
|
 --api
   |
    -- index.go
    -- repo.go
   |
   github.com
   |
   main.go

Environment variables:

$GOPATH : C:\Projects\Go
$GOROOT : C:\Go\

index and repo.go both have the same package name, imports, and just an empty function:

package handlers

import (
    "net/http"
)

func indexHandler(w http.ResponseWriter, r *http.Request) {
}

My main.go:

package main

import (
    "fmt"
    "log"
    "net/http"
    "api/handlers"
)

func main() {
    http.HandleFunc("/api/index", handlers.indexHandler)
    http.HandleFunc("/api/repo", handlers.repoHandler)

    log.Fatal(http.ListenAndServer("localhost:8080", nil))
}
Mr.Smithyyy
  • 2,157
  • 12
  • 49
  • 95
  • 2
    There is no folder `handlers` in `api`. – tkausl Nov 14 '17 at 04:53
  • @tkausl Yes but from what I've read in the previous answers it seems you end with the package name. If I try to just do `api` it removes the error on the import and puts an error on the main that says `cannot refer to unexported name handlers.indexHandler` – Mr.Smithyyy Nov 14 '17 at 04:55
  • I understand the downvotes due to the amount of "duplicates" but I believe that Topo's answer adds some better explanation, especially for the part about the capitalization because I missed that in many tutorials. Things like that are easy to overlook. – Mr.Smithyyy Nov 14 '17 at 05:29

1 Answers1

1

What's happening is that import api/handlers is looking for the folder handlers in the folder api, and then looking in the contents for the package name. Add a handlers folder inside api and move index.go and repo.go into that folder. Or just change the package name to api in those files and just do import api.

About your comment:

cannot refer to unexported name handlers.indexHandler

In order for you to be able to use function indexHandler from your main package, you should rename it to IndexHandler. In go, for things to be able to be accessed by other packages, the name needs to start with a capital letter.

Topo
  • 4,783
  • 9
  • 48
  • 70
  • So the combination of adding the other folder (or just importing api) and then capitalizing the function (which I must have missed completely in the docs) worked. When I read the other answers, I assumed that what you ended the import with was the name of the package not the name of the folder. – Mr.Smithyyy Nov 14 '17 at 05:05
  • 1
    @Mr.Smithyyy Good. the part about capitalizing the letters for exported functions or variables, because it's easy to miss when passing a struct to another package to do something with it, it won't be able to see the fields that are not exported (Start with a capital letter). – Topo Nov 14 '17 at 05:08