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
Related
I am new to golang and trying to import and use modules from this Github repo. https://github.com/Darkbladecr/ccxt/tree/go/go
I am using go module to manage dependencies but importing is failed.
package main
import (
"fmt"
"github.com/Darkbladecr/ccxt/go/pkg/ccxt/models"
)
But I got this error.
go: finding github.com/Darkbladecr/ccxt latest
build command-line-arguments: cannot load github.com/Darkbladecr/ccxt/go/pkg/ccxt/models: cannot find module providing package github.com/Darkbladecr/ccxt/go/pkg/ccxt/models
Is it possible to import a specific directory for go like this repo?
You can either go get the branch containing that package explicitly, or add explicit version tags on that branch in the format that the go tool recognizes (with a leading v prefix on the tags, such as v1.18.445 or v0.1.0-go).
I have an log package in my project src folder. But when I include the log package from an another package as following, the go seams find the log in the system folder instead of my package.
import ("log")
And seams I cannot using relative path to import log package, because go install give following error:
local import "./log" in non-local package
So how can I let the go using my log package?
You need to add your package inside $GOPATH
So if your package is in
$GOPATH/src/github.com/ZijingWu/awesomeapp/src/
your log package would be in
$GOPATH/src/github.com/ZijingWu/awesomeapp/src/log
then it would be possible to use
import("github.com/ZijingWu/awesomeapp/src/log")
The paths seams a bit strange tho, and maybe you should consider adding the log package as a completely separate repository on github, so it would look something like.
import("github.com/ZijingWu/log")
Just using github as an example here, could of course be placed wherever, also just guessing at your username there. Doesnt need to be in github either you can just place it localy on your computer in the $GOPATH, but then noone else would be able to build your package.
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"
)
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.
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
}