Unable to import a package located in the same directory - go

I want to import in the current file or package other file located in the same project in a directory. I'm doing this:
import (
// "./dir1"
"/Users/my_name/my_project/dir1"
)
None of them works
1) Cloning into '/Users/my_name/go/src/github.com/github_username/github_project'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled
2) package /Users/my_name/my_project/dir1: unrecognized import path "/Users/my_name/my_project/dir1" (import path does not begin with hostname)
How to import a directory located in the current project?

Import paths are not directly paths. They are relative to the GOPATH (found by doing echo $GOPATH).
This implies that go is very opinionated about where you store your code as well. So you will have to move your code to $GOPATH/src/my_name/my_project. If you are hosting your code on something like github then move it to $GOPATH/src/github.com/my_github_name/my_project.
Then when you import your sub-packages:
import "github.com/my_github_name/my_project"
Notice that it is not an absolute path.

Rename dir1 to to the same name as the package inside that directory, then you can import it with:
import "./package1"
However doing this is not recommended, use GOPATH instead. If you really don't want to use GOPATH, you may want to use Modules in Go 1.11 (but it is still experimental).

Make sure your project is in GOPATH's go/src folder(Recommended way). Then import like this
package logic
import (
"project_name/folder_name"
)

Related

Any way to prevent using project name from go.mod file in import path

import (
"fmt"
"golang/pkg/closure"
"golang/pkg/player"
"golang/pkg/slices"
"golang/pkg/user"
)
Above you can see my imports in main package. On same level with main.go file , I have folder pkg , where I hold my packages and file go.mod
module golang
go 1.18
How can make it working same way but without using golang/ prefix ?
There is not a way to remove the module path from an import path.
You can, however, remove the pkg folder from your directory structure and import paths.

What is the best way to import local package?

Usually i import local packages by
import "github.com/crsov/myproj/mypkg"
but when i am editing them i need to go get -u "github.com/crsov/myproj/mypkg" every save.
That's the best way to import local package?
I have found many answers to this question but most of them are for older golang versions.
If you have a local package that is very new and has not stabilized, you can do this instead:
go mod init mypkg
and import like this:
import "mypkg"
then you don't have to worry about pulling from the internet, it will just pull from the local directory. Then once your package stabilizes, you can rename so that it can be properly published for others to use.
https://golang.org/doc/code.html

Accessing local packages within a go module (go 1.11)

I'm trying out Go's new modules system and am having trouble accessing local packages. The following project is in a folder on my desktop outside my gopath.
My project structure looks like:
/
- /platform
- platform.go
- main.go
- go.mod
// platform.go
package platform
import "fmt"
func Print() {
fmt.Println("Hi")
}
// main.go
package main
import "platform"
func main() {
platform.Print()
}
go build main.go tells me
cannot find module for path platform
Let me define this first modules are collections of packages. In Go 11, I use go modules like the following:
If both packages are in the same project, you could just do the following:
In go.mod:
module github.com/userName/moduleName
and inside your main.go
import "github.com/userName/moduleName/platform"
However, if they are separate modules, i.e different physical paths and you still want to import local packages without publishing this remotely to github for example, you could achieve this by using replace directive.
Given the module name github.com/otherModule and platform, as you've called it, is the only package inside there. In your main module's go.mod add the following lines:
module github.com/userName/mainModule
require "github.com/userName/otherModule" v0.0.0
replace "github.com/userName/otherModule" v0.0.0 => "local physical path to the otherModule"
Note: The path should point to the root directory of the module, and can be absolute or relative.
Inside main.go, to import a specific package like platform from otherModule:
import "github.com/userName/otherModule/platform"
Here's a gentle introduction to Golang Modules
I would strongly suggest you to use go toolchain which takes care of these issues out of the box. Visual Studio Code with vscode-go plugin is really useful.
Problem here is that Go requires relative paths with respect to your $GOPATH/src or module in import statement. Depending on where you are in your GOPATH, import path should include that as well. In this case, import statement must include go module path in go.mod
GOPATH
Assume your project resides here:
$GOPATH/src/github.com/myuser/myproject
Your import path should be:
import "github.com/myuser/myproject/platform"
VGO
Assume your go.mod file is:
module example.com/myuser/myproject
Your import path should be:
import "example.com/myuser/myproject/platform"
As someone new to go I didn't immediately understand the accepted answer – which is great, by the way. Here's a shorter answer for those very new people!
In go modules/packages are expressed as urls that you import in your code:
import your.org/internal/fancy_module
But wait! My code isn't at a url, what's happening??
This is the cleverness of go. You pretend there's a url even when there isn't one. Because:
This makes including easier as no matter where your file is located the import uses the same url (the code stays the same even if the files move!)
You can have packages that having naming conflicts. So google.com/go-api/user doesn't conflict with the definitions at your.org/internal/user
Someday you might publish a url on GitHub and all the code will just work
That's all great Evan, but how do I import a relative path?
Good question! You can import by changing your go.mod file to have this line:
module fancy_module
go 1.16
replace your.org/fancy_module => ../path/to/fancy_module
Given the Golang Project structure
/
- /platform
- platform.go
- main.go
- go.mod
To access the methods or structs...etc (which are public) from local packages of /platform is simple, shown below
// main.go
package main
import (
p "./platform"
)
func main() {
p.Print()
}
this should work

How to include external file in Go?

I'm using LiteIDE for Go. I have a Go file located here:
/Users/username/go/src/src/Helper/Helper.go
When I include the file using:
import "../Helper"
I get this error:
can't load package: /Users/username/go/src/src/projectA/main.go:4:8:
local import "../Helper" in non-local package
Any ideas what I'm doing wrong?
You import packages by import path. For package Helper, located in $GOPATH/src/Helper/, use:
import "Helper"
While they can work in some cases, relative paths aren't supported by the go toolchain, and are discouraged.

Building package structure with child-/sub-packages

I'm trying to make a simple calculator in Go. I'm designing it in such a way that I can build a command-line interface first and easily swap in a GUI interface. The project location is $GOPATH/src/gocalc (all paths hereafter are relative to the project location). The command-line interface logic is stored in a file gocalc.go. The calculator logic is stored in files calcfns/calcfns.go and operations/operations.go. All files have package names identical to their filename (sans extension) except the main program, gocalc.go, which is in the package main
calcfns.go imports operations.go via import "gocalc/operations"; gocalc.go imports calcfns.go via import "gocalc/calcfns"
To summarize:
$GOPATH/src/gocalc/
gocalc.go
package main
import "gocalc/calcfns"
calcfns/
calcfns.go
package calcfns
import "gocalc/operations"
operations/
operations.go
package operations
When I try to go build operations (from the project dir), I get the response: can't load package: package operations: import "operations": cannot find package
When I try go build gocalc/operations, I get can't load package: package gocalc/operations: import "gocalc/operations": cannot find package
When I try go build operations/operations.go, it compiles fine
When I try to go build calcfns or go build gocalc/calcfns, I get can't load package... messages, similar to those in operations; however, when I try to build calcfns/calcfns.go it chokes on the import statement: import "gocalc/operations": cannot find package
Finally, when I try go build . from the project dir, it chokes similar to the previous paragraph: import "gocalc/calcfns": cannot find package
How should I structure my child packages and/or import statements in such a way that go build won't fail?
Stupidly, I forgot to export my GOPATH variable, so go env displayed "" for GOPATH. (thanks to jnml for suggesting to print go env; +1).
After doing this (and moving the main program to its own folder {project-dir}/gocalc/gocalc.go), I could build/install the program via go install gocalc/gocalc.
Moral of the story, make sure you type export GOPATH=... instead of just GOPATH=... when setting your $GOPATH environment variable
Please try to also add output of $ go env to provide more clues. Otherwise both the directories structure and (the shown) import statements looks OK.
However the sentence
When I try to go build operations (from the project dir), I get the response: can't load package: package operations: import "operations": cannot find package
sounds strange. It seems to suggest you have
package operations
import "operations"
in 'operations.go', which would be the culprit then...?
Very easy:
Lets say I have a project/app named: golang-playground
Put your root dir under GOPATH/src/, in my case GOPATH=~/go/src (run command go env to get your GOPATH). so complete path for my app is ~/go/src/golang-playground
Lets say you want to use function Index() inside of file: router.go from my main.go file (which of course is on root dir). so in main.go:
import (
...
"golang-playground/router"
)
func main() {
foo.Bar("/", router.Index) // Notice caps means its public outside of file
}

Resources