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" const message = "this is examples/metadata"
func main() { func main() {
flag.Parse()
// Set up a connection to the server. // Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithInsecure()) conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil { if err != nil {

View File

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

View File

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

View File

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