I have spent hours trying to read blogs on how to use simple proto file in Golang. I generated the .pb.go files. All internet examples are littered with doing import from some random "github...
urls for proto import. I am not able to find any example on how to import a simple proto file that exists in same dir as my .go file or diff directory. How do I use proto files from local file systems.
go build hello.go
hello.go:5:2: cannot find package "customer" in any of:
/usr/local/go/src/customer (from $GOROOT)
/Users/myhomedir/go/src/customer (from $GOPATH)
Contentes of hello.go
in $SOME_DIR/customer
package main
import (
"fmt"
pb "customer"
)
func main() {
fmt.Println("hello test message
")
}
Contents of customer.proto
syntax = "proto3";
package customer;
// The Customer service definition.
service Customer {
// Get all Customers with filter - A server-to-client streaming RPC.
rpc GetCustomers(CustomerFilter) returns (stream CustomerRequest) {}
// Create a new Customer - A simple RPC
rpc CreateCustomer (CustomerRequest) returns (CustomerResponse) {}
}
// Request message for creating a new customer
message CustomerRequest {
int32 id = 1; // Unique ID number for a Customer.
string name = 2;
string email = 3;
string phone= 4;
message Address {
string street = 1;
string city = 2;
string state = 3;
string zip = 4;
bool isShippingAddress = 5;
}
repeated Address addresses = 5;
}
message CustomerResponse {
int32 id = 1;
bool success = 2;
}
message CustomerFilter {
string keyword = 1;
}
Just put the Customer code in a directory and import it like you would a package
package main
import "$SOME_DIR/customer"
As some of the comments already mentioned, you have to import your customer
file like any other go import - with its full url-like path. That is true even if your main.go
is in the same directory as your dependency. There is a specific way in which everyone should manage go code dependencies and you can read about it here:
It does feel weird to follow a common standard of how to organize your workspace (at least to me it did when I first started with go) - but since you run into errors like yours if you don't follow them; just try to do it.
A pretty good place to start (after you've read 'how to write go code') is actually the error message you got. It will change if you change your import statement/directory structure and it will help you to specify the full path to your dependency in your import ...
statement.