-2

I have a Go project with this structure

src/
    main.go (package main.go)
    www/
        api.go (package apictrl)
        url_mapping.go (package apictrl)
    core/
        model.go (package mlsnd)

As I use Visual Studio Code with WSL Debian to code, I get the error undefined: apictrl when I write apictrl.StartRouter() in my main.go file.

I tried the following :

  •    import (
              "../www"
      )
    
  •   import (
              "../www/apictrl"
      )
    
  •   import (
              "../apictrl"
      )
    
  •   import (
              "/mnt/c/Users/myusername/path/toproject/src/apictrl"
      )
    
  •   import (
              "/mnt/c/Users/myusername/path/toproject/src/www/apictrl"
      )
    
  •   import (
              "/mnt/c/Users/myusername/path/toproject/src/www"
      )
    

As I saw in this post Import package & type but none of them worked.

Thryn
  • 425
  • 2
  • 14
  • 4
    Every one of your import examples is invalid. `../www` is an invalid import path. [Go does not support relative imports](https://stackoverflow.com/questions/38517593/relative-imports-in-go). The paths beginning with `/mnt` don't work because Go expects an import path that begins with a hostname, not a file path. – Jonathan Hall Feb 11 '21 at 10:57
  • 4
    Please read [How to write Go code](https://golang.org/doc/code.html) – Jonathan Hall Feb 11 '21 at 10:58
  • 4
    As for the title of your question, this seems to be a red herring. A package's name has absolutely nothing to do with the filename. By convention, a package's name is the same as it the name of the directory containing it, but this is not a hard rule. – Jonathan Hall Feb 11 '21 at 11:00
  • Thanks, so I have to host my code on github/lab ? Or open an apache server on the machine I want it to work on ? – Thryn Feb 11 '21 at 11:06
  • 3
    GitHub/GitLab are easy ways to get started. But you can host it anywhere--or nowhere. If you host it nowhere, you'll only be able to use it from the same project (perfectly fine in many cases), but you still need to give it an appropriate name in your `go.mod`. You could use a hostname like `local.local` if you're sure you'll never host it somewhere. – Jonathan Hall Feb 11 '21 at 11:16
  • 3
    "Note that you don't need to publish your code to a remote repository before you can build it. A module can be defined locally without belonging to a repository. However, it's a good habit to organize your code as if you will publish it someday." One of the first paragraphs of "How to write Go code". Really, read it. – Peter Feb 11 '21 at 11:54

1 Answers1

3

So as comment pointed out, I found out the solution :

First, I had to rename all folders to have the name of the package they contained

src/
    main/
        main.go (package main)
    apictrl/
        api.go (package apictrl)
    mlsnd/
        model.go (package mlsnd)

In the main.go I had to import the wanted package using the name given to the module in go.mod (created with go mod init package.name)

import (
    "package.name/src/apictrl"
) 
Thryn
  • 425
  • 2
  • 14