internal: example flag.Parse() and comments (#2526)

This commit is contained in:
Menghan Li 2018-12-20 12:43:38 -08:00 committed by GitHub
parent d9bc55b745
commit 463950a151
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 8 deletions

View File

@ -284,6 +284,7 @@ func bidirectionalWithMetadata(c pb.EchoClient, message string) {
const message = "this is examples/metadata"
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {

View File

@ -194,6 +194,7 @@ func (s *server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamin
}
func main() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {

View File

@ -16,10 +16,12 @@
*
*/
// Binary client is an example client.
package main
import (
"context"
"flag"
"fmt"
"log"
"time"
@ -29,9 +31,7 @@ import (
hwpb "google.golang.org/grpc/examples/helloworld/helloworld"
)
const (
address = "localhost:50051"
)
var addr = flag.String("addr", "localhost:50051", "the address to connect to")
// callSayHello calls SayHello on c with the given name, and prints the
// response.
@ -56,8 +56,9 @@ func callUnaryEcho(client ecpb.EchoClient, message string) {
}
func main() {
flag.Parse()
// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure())
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}

View File

@ -16,10 +16,13 @@
*
*/
// Binary server is an example server.
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
@ -30,9 +33,7 @@ import (
"google.golang.org/grpc/status"
)
const (
port = ":50051"
)
var port = flag.Int("port", 50051, "the port to serve on")
// hwServer is used to implement helloworld.GreeterServer.
type hwServer struct{}
@ -61,10 +62,13 @@ func (s *ecServer) BidirectionalStreamingEcho(ecpb.Echo_BidirectionalStreamingEc
}
func main() {
lis, err := net.Listen("tcp", port)
flag.Parse()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
fmt.Printf("server listening at %v\n", lis.Addr())
s := grpc.NewServer()
// Register Greeter on the server.