Next we need to generate the gRPC client and server interfaces from our .proto service definition. We do this using the protocol buffer compiler protoc with a special gRPC Go plugin.
For simplicity, we’ve provided a bash script that runs protoc for you with the appropriate plugin, input, and output (if you want to run this by yourself, make sure you’ve installed protoc and followed the gRPC-Go installation instructions first):
[Read More]
Concurrency and Parallelism
Concurrency The composition of independently executing tasks. Applied when dealing with handling lots of things at once. The focus is on how to structure a solution to solve a problem which may or may not be solved in a parallel manner. Concurrency is a way to structure a program by breaking it into pieces that can be executed independently. Communication is the means to coordinate the independent executions. Go supports concurrency.
[Read More]
Building a gRPC Client in Go
In this section, we’ll look at creating a Go client for our RouteGuide service. You can see our complete example client code in grpc-go/examples/route_guide/client/client.go.
Creating a stub To call service methods, we first need to create a gRPC channel to communicate with the server. We create this by passing the server address and port number to grpc.Dial() as follows:
conn, err := grpc.Dial(*serverAddr) if err != nil { ... } defer conn.
[Read More]
My VS Code Congig for Go
{
"go.lintTool": "golangci-lint",
"go.formatTool": "goimports",
"go.useLanguageServer": true,
"go.lintOnSave": "package",
"go.vetOnSave": "package",
"go.vetFlags": [
"-all",
"-shadow"
]
}
Building a gRPC Server in Go
Let’s start off by defining a really simple gRPC server in Go. Once we have a simple server up and running we can set about creating a gRPC client that will be able to interact with it.
We will start off by writing the logic within our main function to listen on a port for incoming TCP connections:
main.go
package main import ( "log" "net" ) func main() { lis, err := net.
[Read More]
Create greet package
Most main editors have some support for Go. Check the full list of IDEs and Plugins for Go on the official Go Wiki.
Though many editors support Go, they do not offer the same level of features and integrations.
These are things you might look for when considering an editor, from the most basic to the more advanced use cases. Keep in mind people tend to have different opinions here, so this is just a list to let you know some of what is possible, value the ones you care about most:
[Read More]
Cobra - Create CLI in Go
Golang for CLI Go is the perfect language to develop command line applications. Go has a few advantages that really set it apart from other languages:
Single binary Very fast execution time, no interpreter needed Go is awesome! Cross platform support Command line based applications are nearly as old as computing itself but this doesn’t mean that they haven’t evolved. Traditional cli applications used flags to manage the different behaviors an application could perform.
[Read More]
Create the first test for your program
The convention in Go is to put unit tests alongside the code that they test rather than in a different directory. This has the advantage of giving a very visible way of seeing what has tests and what doesn’t. All test files are named *_test.go; anything named that way will not be added to the final binary, but will be taken into account by go test.
Create a file main_test.go and add the following code:
[Read More]
Create your first Go program
Lets write our first hello world program.
(1) Create a src directory in $GOPATH, which is /root/gowork in my system:
# mkdir src # tree . └── src 1 directory, 0 files (2) Since Go organizes source code using “package” concept , and every “package” should occupy a distinct directory, I create a greet directory in src:
Create a directory in your workspace:
mkdir -p $GOPATH/src/github.com/<your_user>/hello cd $GOPATH/src/github.com/<your_user>/hello touch main.go Open your favorite editor, and add the following to main.
[Read More]
Defining gRPC service
Our first step (as you’ll know from the quick start) is to define the gRPC service and the method request and response types using protocol buffers. You can see the complete .proto file in examples/route_guide/routeguide/route_guide.proto.
To define a service, you specify a named service in your .proto file:
service RouteGuide { ... } Then you define rpc methods inside your service definition, specifying their request and response types. gRPC lets you define four kinds of service method, all of which are used in the RouteGuide service:
[Read More]