packagemain
import(
"context"
"fmt"
"log"
"rpc/grpc/user"
"time"
"google.golang.org/grpc"
)
funcmain() {
//建立链接
conn, err := grpc.Dial("127.0.0.1:1234", grpc.WithInsecure())
iferr != nil {
log.Fatal("did not connect", err)
}
deferconn.Close()
userClient := user.NewUserClient(conn)
//设定请求超时时间 3s
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defercancel()
//UserIndex 请求
userIndexResponse, err := userClient.UserIndex(ctx, &user.UserIndexRequest{
Page: 1,
PageSize: 12,
})
iferr != nil {
log.Printf("user index could not greet: %v", err)
}
if0 == userIndexResponse.Err {
log.Printf("user index success: %s", userIndexResponse.Msg)
// 包含 UserEntity 的数组列表
userEntityList := userIndexResponse.Data
for_, row := rangeuserEntityList {
fmt.Println(row.Name, row.Age)
}
} else{
log.Printf("user index error: %d", userIndexResponse.Err)
}
// UserView 请求
userViewResponse, err := userClient.UserView(ctx, &user.UserViewRequest{Uid: 1})
iferr != nil {
log.Printf("user view could not greet: %v", err)
}
if0 == userViewResponse.Err {
log.Printf("user view success: %s", userViewResponse.Msg)
userEntity := userViewResponse.Data
fmt.Println(userEntity.Name, userEntity.Age)
} else{
log.Printf("user view error: %d", userViewResponse.Err)
}
// UserPost 请求
userPostReponse, err := userClient.UserPost(ctx, &user.UserPostRequest{Name: "big_cat", Password: "123456", Age: 29})
iferr != nil {
log.Printf("user post could not greet: %v", err)
}
if0 == userPostReponse.Err {
log.Printf("user post success: %s", userPostReponse.Msg)
} else{
log.Printf("user post error: %d", userPostReponse.Err)
}
// UserDelete 请求
userDeleteReponse, err := userClient.UserDelete(ctx, &user.UserDeleteRequest{Uid: 1})
iferr != nil {
log.Printf("user delete could not greet: %v", err)
}
if0 == userDeleteReponse.Err {
log.Printf("user delete success: %s", userDeleteReponse.Msg)
} else{
log.Printf("user delete error: %d", userDeleteReponse.Err)
}
}