-2

I'm really new to Go and I'm trying to import a local file in the main.go. This is how the project is structured:

-project_name(dir)
--src(dir)
---main.go
---auth(dir)
----signup.go

I'm trying to use a function I wrote in signup.go in main.go. This is how I tried to import the signup.go in main.go:

import (
  "encoding/json"
  "fmt"
  "log"
  "net/http"

  "github.com/gorilla/mux"
  auth "./auth"
)

and use it like that:

myRouter.HandleFunc("/signup", auth.signupUser).Methods("POST")

I also tried copying the relative auth dir and write it in the import but I still get an error undefined: auth go

I tried looking for answers but I can't see what I'm doing differently from the answers I saw.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
John Doah
  • 1,839
  • 7
  • 25
  • 46
  • Add `project_name` to `GOPATH` environment variable, perhaps these will help [Relative Imports in golang](https://stackoverflow.com/questions/38517593/relative-imports-in-go) and [Multiple Directories Gopath](https://stackoverflow.com/questions/36017724/can-i-have-multiple-gopath-directories) – Gaurav Dhiman Jan 17 '20 at 20:02
  • 2
    You do not import files in go, you only import packages. Do not use relative imports for packages. Start with [How to Write Go Code](https://golang.org/doc/code.html), which explains all the details necessary to create a simple package and import it. – JimB Jan 17 '20 at 20:02
  • @GauravDhiman Please correct me if I'm wrong, but from what I understood, I need to create my project in the GOPATH. So if my go directory (and GOPATH) is at /home/go, I need to create all of my project there? Because I have a different directory for all my projects and it's not /home/go. – John Doah Jan 17 '20 at 20:10
  • @JohnDoah not necessarily, you can add multiple directories in `GOPATH`, however keep your projects organized i wouldn't recommend adding different directory for each project in `GOPATH` – Gaurav Dhiman Jan 17 '20 at 20:12
  • 2
    `auth.signupUser` is an unexported identifier so you won't be able to use it from another package. Please take the [Tour of Go](https://tour.golang.org/) to get a good overview of the language fundamentals. – Adrian Jan 17 '20 at 20:28
  • Use go modules. You don't need to create your project under `GOPATH` anymore: https://blog.golang.org/using-go-modules – Burak Serdar Jan 17 '20 at 20:28

1 Answers1

-1

Relative imports are not recommended in Go.

If project_name is inside your GOPATH, like $GOPATH/github.com/john/project_name, you can import auth like this:

import "github.com/john/project_name/src/auth"

If you are using Go modules, then instead of github.com/john/project_name, use whatever name is inside your go.mod. If you are not using Go modules but would like to, you can do go mod init github.com/john/project_name whilst inside the project_name folder, and then that folder will be used as a base.

Note that signupUser has to actually be SignupUser to be exported (and therefore useable).

qaisjp
  • 722
  • 8
  • 31