簡體   English   中英

在項目中導入 pb.go 文件

[英]Importing pb.go file in a project

如何導入生成的pb.go文件,以便調用其中的方法? 我訪問了 go 的所有示例,它們似乎都是使用github.com/...導入的github.com/...

例子.go

import (
    "github.com/go-chi/chi"
    chiMiddleware "github.com/go-chi/chi/middleware"
    "google.golang.org/grpc"
    "net/http"
    "github.com/easyCZ/grpc-web-hacker-news/server/hackernews"
    hackernews_pb "github.com/easyCZ/grpc-web-hacker-news/server/proto" // Proto directory.
    "google.golang.org/grpc/grpclog"
    "github.com/improbable-eng/grpc-web/go/grpcweb"
    "github.com/go-chi/cors"
    "github.com/easyCZ/grpc-web-hacker-news/server/proxy"
    "github.com/easyCZ/grpc-web-hacker-news/server/middleware"
)

func main() {
    grpcServer := grpc.NewServer()
    hackernewsService := hackernews.NewHackerNewsService(nil)
    hackernews_pb.RegisterHackerNewsServiceServer(grpcServer, hackernewsService)
    ...

目錄結構

📦athena-server
 ┣ 📂proto
 ┃ ┣ 📜athena.pb.go
 ┃ ┗ 📜athena.proto
 ┣ 📂server
 ┃ ┗ 📜server.go
 ┣ 📜.DS_Store
 ┣ 📜docker-compose.yml
 ┣ 📜generated.go
 ┣ 📜go.mod
 ┣ 📜go.sum
 ┣ 📜google_service.json
 ┣ 📜gqlgen.yml
 ┣ 📜models_gen.go
 ┣ 📜resolver.go
 ┣ 📜schema.graphql
 ┗ 📜todo.go

main.go

import (
    "bytes"
    "flag"
    "fmt"
    "log"
    "net/http"
    "os"

    "google.golang.org/grpc"
    "google.golang.org/grpc/grpclog"

    "github.com/improbable-eng/grpc-web/go/grpcweb"

    auth "../proto" // Proto directory but doesn't work.
)
...

錯誤

cannot find module for path _/Users/xxx/go/src/athena-server/proto

我嘗試了所有排序絕對和相對路徑以及移動文件,但似乎沒有任何效果。

可以做一個go mod init project ,然后導入位於某個文件夾中的 go 文件,例如文件夾為:

import "project/folder"

我創建了幾個包含這種結構的github 項目,例如 n3dr、bcbsn、go-yq、go-multipart 和 dip。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM