如何使用gRPC创建rest API以读取go lang中的文本或任何文件?

I am trying to create a REST API in go programming language, to read the text file.

I want to build it using gRPC.

You could follow the Go introduction that's in the gRPC docs.

The basic process is as follows:

1) Write the Protocol Buffers definitions for your data structures and a gRPC service definition that integrates them:

package fileserver

message FileRequest {
  string name = 1;
}

message FileResponse {
  bytes contents = 1;
}

service FileServer {
  rpc ReadFile (FileRequest) returns (FileResponse) {}
}

2) Generate the gRPC/PB bindings using the "protoc" tool (you will need "protoc-gen-go" as well). The command is usually:

protoc --go_out=plugins=grpc:. *.proto

3) Implement the server:

type FileServer struct{}

func (s *FileServer) Dispatch(ctx context.Context, fileRequest *fileserver.FileRequest) (*fileserver.FileResponse, error) {
  f, _ := ioutil.ReadFile(fileRequest.Name)
  fileResponse := &fileserver.FileResponse{Contents: f}
  return fileResponse, nil
}