Import from the same package, using package name - go

I've added dependency to https://github.com/sjwhitworth/golearn in Golang project. I've tried to build application, however I am getting error from one of the classes from golearn - this one: https://github.com/sjwhitworth/golearn/blob/master/base/dataframe_go.go : undefined: base. I've tried to find what is the base package refering to. There is no imported package base in this file so it looks like it is import from the same package but using the package name. I mean, dataframe_go.go is in the same package (base) where is the imported struct (FixedDataGrid). How to solve this import issue?

Related

Conflict in repo name and module name

I created the github.com project qjson/qjson-go that contains the package name qjson that you can see here. I named the github project this way because github.com/qjson/ contains other projects for different languages (e.g. qjson-c).
Unfortunately, I get the following error when I try to import the project as github.com/qjson/qjson-go:
$ go mod tidy
go: finding module for package github.com/qjson/qjson-go
go: downloading github.com/qjson/qjson-go v0.0.0-20210128102242-170c47e2db46
github.com/xxx/xxx imports
github.com/qjson/qjson-go: module github.com/qjson/qjson-go#latest found (v0.0.0-20210128102242-170c47e2db46), but does not contain package github.com/qjson/qjson-go
I’m apparently doing it wrong. I understand that due to the import statement we are then expected to use gjson-go as package identifier.
What must I do so that the git project can be named qjson-go and the package qjson ?
I assume that one solution is to create a sub-directory named qjson inside qjson-go and move all package files in it. The user would then import "github.com/qson/qson-go/qjson". Is that correct ? Is there another solution avoiding the stutter ?
This program works as expected:
package main
import (
"fmt"
"github.com/qjson/qjson-go/qjson"
)
func main() {
fmt.Println(qjson.ErrDivisionByZero)
}
The issue is that you are using this file structure:
qjson/engine.go
qjson/errors.go
When you should just be putting them at the top level, like this:
engine.go
errors.go
So you can either fix the directory, and tag a new version, or just leave the
files as is, and change your imports to match what I have above.

Could not import ... (no required module provides package)

I am following this tutorial.
I initialised my project and got the dependencies via
go mod init github.com/martin-helmich/kubernetes-crd-example
go get k8s.io/client-go#v0.17.0
go get k8s.io/apimachinery#v0.17.0
I have a Go file that uses import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1".
VS Code shows me could not import k8s.io/apimachinery/pkg/apis/meta/v1 (no required module provides package "k8s.io/apimachinery/pkg/apis/meta/v1"). What am I doing wrong here? Is my Go installation not correct?

Go module import from github

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).

how can i use a function inside main package from a file in another package in GO?

Hi I want to call a method inside the main package, my project struct is like this:
src:
go files : package main
Postgres folder:
go files: postgres package
now I want to call a method inside main package from the go files inside the postgres folder that is from postgres package.
I tried to import "foo/src"
then using src.Myfunction but I got an error:
import "foo/src" is a program, not an importable package
The package main is supposed to be used only to implement the binary/command specific code. It usually imports code from other packages to glue everything together. If you need to import something from the package main, that code is probably not specific to that command, so it should belong to another package. After you refactor the code, you can import it from the package main and from your other package which also requires it.

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.

Resources