5

I want to push out some of API, from main package into separate package:

myapp/
    main.go
    myapi/
        myapi.go

Inside main.go i have

package main

import "./myapi"

...

And myapi.go just starts with:

package myapi

...

When I am trying to run main, it seems like it can't find my myapi #include. It gives me following error:

D:\go\myapp> go run .
build _/D_/go/myapp/myapi: cannot find module for path _/D_/go/myapp/myapi

I came from C/C++ world, and it's extremely unobvious, how to include from subfolder in golang. Could you help me with this?

xakepp35
  • 2,878
  • 7
  • 26
  • 54
  • 3
    [Do not use relative imports](https://stackoverflow.com/questions/38517593/relative-imports-in-go). – Jonathan Hall Dec 21 '19 at 15:15
  • 1
    You can use a module file that defines the name of your module and its submodules – xarantolus Dec 21 '19 at 15:21
  • @xarantolus How? Given above sample program, what actions should I perform? – xakepp35 Dec 21 '19 at 15:23
  • 1
    @xakepp35: What is non-sense? Using the Go as intended? – Jonathan Hall Dec 21 '19 at 19:05
  • 1
    @xakepp35 you should run `go mod init your.domain/somename` in your package root (`myapp`), it will create a file called `go.mod`. Now you can import `your.domain/somename/myapi` to access your package. When you move the root folder somewhere else, you can still build it. You could even build it without downloading packages on another pc by [vendoring your dependencies](https://stackoverflow.com/q/58697521) – xarantolus Dec 22 '19 at 08:48
  • @xarantolus `gcc` is always on the system, so in my imaginary nuclear disaster, i can fallback using it. So here goes my that upper question.. Can golang be like so? Local only! Or.. would it require constant internet connection, thus unusable by profesional devs? – xakepp35 Dec 23 '19 at 04:58
  • After you set up the module (the name is usually a domain, but the build process won't try to connect to it; it's just a name), you can run `go mod vendor` in your root directory to download all dependencies. As I explained in [this answer](https://stackoverflow.com/a/58700267), it is possible to use go completely offline after you downloaded your dependencies – xarantolus Dec 23 '19 at 07:20
  • @xakepp35: Bad analogy. A better analogy would be when someone asks "How do I use a bazooka to kill a fly?" and the answer is "Don't use a bazooka to kill a fly." – Jonathan Hall Dec 23 '19 at 08:32

1 Answers1

4

Go uses something called Module Paths. These are paths that identify your packages. They are not necessarily related to the file system.

An example of a module path is github.com/hajimehoshi/ebiten.

If you're using Go modules, this is also the path Go automatically downloads it from.

If you're using $GOPATH, the path into the source of the module would be go/src/github.com/hajimehoshi/ebiten.

Initialize your module with a new module path using go mod init <module path>. Generally this will be your GitHub repository, without the https://. This will allow for both your and others code to be able accessible using that module path. myapi should then be accessible via import "github.com/username/repo/myapi".

If you still wish to use the old $GOPATH method, simply place your code inside go/src/<module path>. The method of accessing myapi is equivalent.

Read Using Go Modules and How to write Go code for more information.

Darker Bit
  • 41
  • 1
  • 1
    Thanks for expla, i've upvoted your effort. For me, as a man of 20y of programming experience, golang is like C with some influences. Like Borland's one(Currently, Embarcadero) modules experience. And C.. But that was a real good thing(if you've used to know things like Delphi 7, or latter RAD) - that allowed us not to bother with global stuff. It's like you may define some local module, that is just within your subfolder, and use it with ease. – xakepp35 Dec 23 '19 at 04:42
  • 1
    If you've survived a nuclear war, and you're using Go, you'll still use Go as it's designed. – Jonathan Hall Dec 23 '19 at 08:41