Golang gRPC服务器流

I have some issue with a gRPC server-stream on golang. I had no problem with one row, I just used the simple gRPC response for it. But now I need to send a number of rows from my database and I can't finish my server-stream application.

I just learn Golang and gRPC and this task a little bit difficult to me to solve this task now. And I will be very grateful if someone could help with this because not too many examples of this material on the web. Or maybe you now where I can find an example, how to stream data from database using gRPC + golang. Thank you

I have this code:

....

type record struct {
id       int
lastname string
}

type server struct {

}

func (s *server) QueryData(city *pb.City, stream pb.GetDataStream_QueryDataServer) error {

db, err := sql.Open("postgres", "postgres://adresspass")
if err != nil {
    log.Fatal(err)
}

defer db.Close()

rows, err := db.Query(`SELECT id, last_name FROM  person WHERE city=$1`, city.City)
if err != nil {
    log.Fatal(err)
}

var rs = make([]record, 0)
var rec record

for rows.Next() {
    err = rows.Scan(&rec.id, &rec.lastname)
    if err != nil {
        return err
    }

    rs = append(rs, rec)
}

for _, onedata := range rs {
    if err := stream.Send(onedata); err != nil {
        return err
    }
}
return nil
}

...

You should scan your values from database as protocol buffer types and not native types.

You should have defined a message format in proto file

message DbVal{
 uint32 id = 1;
 message string = 2;
}

And pass that struct to the stream.