Specify base package in protobuf compilation in golang - go

I've got golang package github.com/user/protoapp, in this package I have folder proto containing protobuf files.
github.com/user/protoapp
|-proto
|-proto/app1
|-proto/app2
proto files in app1 and app2 have corresponing packages app1 and app;
Proto file from proto/app1 is importing file from proto/app2 like import "app2/messages.proto"; after compillation in app1.pb.go it becomes import app2 and protoapp fails to compile. How do I make imports in *.pb.go files become import "github.com/user/protoapp/proto/app2" rather than import "app2"?

you need to make your import in your proto a fully qualified path like in Go:
instead of import "app2/messages.proto";
try import "github.com/user/protoapp/proto/app2/messages.proto";

Never specify the half path "/app2/messages.proto" it won't be work.
Specify the full import name
import "github.com/user/protoapp/proto/app2/messages.proto";
and
Do define with the package name
//if we does'nt add package we define it will show error
package messagedata;

Related

Use Absolute path instead for go "fmt" in import directive

This is an instructional question and not a procedural one as simply requiring "fmt" works just fine, but when with the hello world golang file I modify it as follows
package main
import "golang.org/fmt"
func main() {
fmt.Println("Hello, world")
}
I get in response:
go:3:8: no required module provides package golang.org/fmt; to add it:
go get golang.org/fmt
I can see the fmt package in /usr/local/go/src/fmt and it mirrors the files in https://golang.org/src/fmt/
I am probably very close in the above file, what is the correct absolute path that would work to include fmt ? Thank you!
The correct absolute import path for the package is fmt.
Relative import paths start with ./ or ../. The import path fmt is an absolute import path.
Remote import paths start with a domain name. The package does not have a remote import path.
The tool chain creates a unique package for each import path. If the application could refer to the source code for the fmt package using a remote import path, the package with the remote path will be different from the standard fmt package. Every aspect of the package is unique. The code is duplicated. There is a ScanState type for each package and these types cannot be used interchangeably.
The pp cache is duplicated. And so on.
In this case, fmt is the fully qualified path. Compare the fmt docs [1] with golang.org/x/text docs [2].
Go standard library does have a go.mod [3], but trying to import std/fmt doesn't work either.
https://pkg.go.dev/fmt
https://pkg.go.dev/golang.org/x/text
https://github.com/golang/go/blob/master/src/go.mod

Import from the same package, using package name

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?

i am not able to import a package from another directory to main.go

while i tried adding import config "./config" in main.go and try to save it and run then it remove the import config "./" section
my code structure is
-config/config.go
model/model.go
main.go
while running i got
[go] can't load package: package .: found packages config (config.go) and main (main.go) in /Users/Desktop/inventory-backend
In go we import modules by specifying the path as e.g.
import "gopath/projectName/config"

Unable to import a package located in the same directory

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

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