golang cannot find module for local path

11,701

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.

Share:
11,701
xakepp35
Author by

xakepp35

I do love everything about PC; From power generation plants, electricity and transistors, to designing, developing, optimizing, testing and using complex software stacks. I love microcontrollers, CPUs, GPUs and crafting cases, racks, and even cooling systems for datacenter rooms. I love bios modding, asm, C, C++, Javascript, Golang. I wrote 16-bit programs in turbo pascal, back in school times, and saw Windows 3.11. I've played with visual basic, and used C-rootkits for winserver2003. I've met times of renaming Delphi 7 to Rad Studio. I knew Visual C++ 6.0. I built linux kernels and ramfs for embedded systems. I've smiled with Gentoo, laughed at X.Org, and used Freebsd ports, to build chromium and vlc on a slow LGA775 platform, thats infinite story.. Only thing I hate - is Apple: macbook, osx, iphon, stevjobs, et al.. These are worst things in the present universe and history of IT! Dont even mind to name it when children are nearby!!!

Updated on June 04, 2022

Comments

  • xakepp35
    xakepp35 almost 2 years

    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
    xakepp35 over 4 years
    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.
  • Flimzy
    Flimzy over 4 years
    If you've survived a nuclear war, and you're using Go, you'll still use Go as it's designed.