Building a Basic RPC Server and Client with Go

Tensor Programming · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

This video demonstrates building a basic RPC server and client using Go, covering RPC library, net RPC library, and Go tools, with a focus on CRUD application and API design.

Full Transcript

hey guys my name is tensor welcome to another go tutorial video today we're going to be looking at how we can create an RPC server our PC stands for a remote procedure call and essentially it's exactly what it sounds like basically we want to make it so that a external computer can call to our program interface and execute various pieces of our code before we actually build this server though let's create a basic crud application for this application let's first start out by creating an item struct this is just a nondescript data type that we're going to use in our crud application it has two fields a title and a body both of which are strings we can then use this item struct to create a makeshift database by just creating a slice of item type in memory so first let's start by creating the read function and we'll make it so that we can read through our database by getting the items by their names so the function will take in a string and then it will return the corresponding item we want to create a variable which will be our get item this would be the item that we're pulling out of the database and then we can use a for loop to iterate through the values in the database and as we're iterating through our database we'll just check to see if the value title is equivalent to the title string that we're passing in here and if it is then we just take that value and we assign it to our get item item and then finally at the end of the function we just return the get item variable now let's build our create function this will just take in an item and then it will pass back an item and the idea is that we pass in the item and then it gets put into the database and then we just return the item so that we know which item we've added to the database so all we have to really do here is append our database with the item and then return the item from the function now let's create a function called add item it just takes in an item and then it outputs an item and we essentially just penned that item to our database and then returned the item from the function that way we know which item we've just added to the database to create our edit item function we want to pass in the title of the item that we want to get from the database and then we also want to pass in a new item that we want to replace the old item with and of course like with our other functions we'll return an item from this function we can iterate through the database using range this time we'll grab the index and then the value and again we'll check to see if the values title is equal to our edit items title if it is we'll grab the item by its index and we'll set in the new edit item which will replace the original item and then we'll set that edit item into this changed variable so that we can return it from the function now let's create a delete function so this just takes in an item and then it will return the item that gets deleted without delete function will iterate through the database grabbing the index and the value and then we'll check to see if the title and the body are equal to the item that we're pulling out of the database and to remove the item from our slice we can just use the append spread operator and we can take the database all the way up to the index and then the database from after the item that we want to remove all the way to the end so this actually just creates a new database without the item that we want to remove then we can take the item that we're passing through this function put it into this elite variable and then return it from the function now inside of our main function let's do some test operations though first let's print out the initial database which will be empty then we can create three items and add them to the database and then print out the database yet again and we'll delete the middle item which is second a second item and then of course print out the database then we'll go ahead and edit an item which will be our third item and to edit the item we just pass in the title in the item that we want to edit followed by the item that we want to replace this item with and then we'll print out the database after that and then finally we'll use our get function to get the new item and then the original first item and then we'll print the two out here's what it looks like if we run this in our command prompt you can see our initial database is empty and our second database has three different objects inside of it and our third database has only two objects because we deleted the second one and then our fourth database has two objects in it because we've changed the third one and then finally we print out those two items by getting them from the database so this application suffice --is as a crud application now what we essentially want to do is make it so that we can call all of these functions remotely using the RPC library the net RPC library in go kind of stipulates that the functions need to satisfy various different criteria firstly the functions need to be a method so none of our functions satisfy this criteria because all of them are functions not methods these functions all need to be exported which means they all need to have uppercase letters which all of our functions do adhere to functions need to have two arguments both of which are exported types and this of course includes built-in types as well so the get by name function for instance can't be called as an RPC because it only has one argument our Edit item function does fit this however because it takes in a string and it also takes in an item and the item is exported because it's an uppercase letter for the structure the next stipulation however kind of excludes all of our functions and that is that the second argument for the function must be a pointer the final stipulation is that the return type for our functions for the RPC functions needs to be an error so now let's go ahead and shape our functions so that they all fit these criteria and to make this work we'll define a new type called API which will be just an integer and what we can do with this API type is we can use it to essentially elevate all of our functions to methods so they'll all be methods on this API type so first with our get by name function we'll turn it into a method by adding the receiver for the API pointer next we do need to change the arguments for this function however because it doesn't have to and because the second one isn't a pointer now the idea is that the first argument represents the argument which were passing through by the color and then the second argument represents the results of calling this function so the second argument is the result that we're returning to the client that's calling to this API so in the case of our get by a name function we'll keep the first argument as the title string and then we'll have a second argument which will be a reply and it will be a pointer to an item type we also need to make sure that the return type for this method is an error type so we'll go ahead and do that and you'll see that of course we'll get an error down here because it's returning an item now the reason why we want an error type to be returned is because if an error type gets returned from this function then that means that the RPC will not send back any data to the caller on the outside this method follows the RPC stipulations that we talked about before but of course we do need to change the internal logic so that it makes sense with this new method signature we can do this rather easily for this particular function by simply taking our reply pointer and setting it equal to the get item variable which we have here so essentially we iterate through the database we find the value where the title string is equivalent to that value we put that into this item variable and then we pass it back to the color through the reply argument and also because we're not really concerned about errors in this particular tutorial we'll just return nil which satisfies the error type return type for this method now let's modify our add item function so we need to add the receiver for the API type and then of course we need to edit the signature so that it has two arguments the item that we want to add to the database and then the reply which will be the item that we've added to the database and then finally of course we need this to return an error again this is fairly simple we just add a new line where we take the reply pointer and we set it equal to the item that's being passed into this method and then of course we return nil for the Edit item method we need to add the receiver then we also need to change the Edit item arguments so that they fit the formula that we've created so thus far we've made it so that we have the item that's being passed by the caller and then they return data so we have a string here and then we have our item that we want to edit instead what we'll do is we'll remove this title string and then of course we'll add our reply pointer and finally of course we want this to return an error so now when we iterate through the database instead of checking to see if the valve title is equal to the string that we were passing through here we can just check to see if it's equal to the Edit title of the item that's being passed through this function and inside of the body we'll take the databases index item and then we'll just create a new item and replace that old item and the new item just takes the Edit items title and body and then uses that to create our new item and of course then we'll grab that item from the database using the index and we'll pass it into our changed variable and like with the other functions before it we can just use the pointer for reply and set that equal to changed and then return nil for the error the delete item function is going to be very similar to the Edit item function or first make it a method of our API type then we need to add a second argument to the method or the reply pointer and then of course we need to return an error in the function body the only thing that we need to change is the return statement and we need to make it so that we take our delete variable and put it into the reply and now all of our methods are ready to be called remotely through our client so let's now create a server inside of the main function so that we can actually connect to these methods first we want to create a new API type so that we can call all the methods on this API type we can just use the new function to do this this variable is important because we want to use it in this RPC register method and that way we essentially register the type so that we can call its methods remotely we can deal with the error that potentially could come from registering this type by just calling wrong fatal and then saying that we got an error registering the API now we want to register an HTTP handler and we can use our PC and/or HTTP to do that then to actually open the connection we want to use net listen and we're going to be opening the connection over our tcp with a port of 4040 of course we can also handle the error that could potentially come from calling this net listen method then finally to serve the listener that we've created here we can just come and call HTTP served passing our listener and then narrow for the second argument and then we'll handle the errors for that as well and we'll print out above it that we're serving our RPC on poor 4040 if we go into our command prompt you can see that this actually runs and it says that we're serving the RPC on port 40 40 now let's go ahead and create a client application so that we can access the RPC so I've got a folder here called client and then I'm gonna just create a main guy go file inside of it and of course this is its own application so it will be also a package main with a main function in this file we do need to define the item type because this is the type that we're going to catch they're receiving data with where larger applications it's more common to actually share the types by creating some kind of library that you can depend on for both the server and the client but in our case we'll just recreate the type here that way we won't run into any problems inside of our main function let's create a reply item and then we'll create a makeshift database which will be a slice of items now we can go ahead and Dow into our RPC by calling RPC Dao HTTP passing in the type which will be TCP and then passing in the address which will be localhost 4040 this will give us back the client and then an error potentially so we need to handle that error now like with before when we were just running everything locally let's go ahead and create a few items that we can manipulate between our client and our server and then to actually execute the methods from our client we can use this client call method and we want to pass in a string representation of the method that we want to execute followed by the two arguments for the method so here I'm calling the add item method with our a item inside of it and I'm doing it again 4b and 4c and then I'm also using the reply item which we created up here as our return statement for each of the items that were passing to the server we get back item in this reply reference unlike our local program however we can't directly access the database to actually see if these items were pushed into the database now let's go back into our server and create a function which will allow us to Rab the database through the client this method is actually pretty basic notice I've put in a first argument even though we don't really need one that's because the general structure for this function needs to have a first argument regardless so I just put in a title string though we don't really need to pass anything to this function instead what just happens is it grabs the database and then it passes it to the reply pointer which then sends it back to our client so now in this case I can call client dot call from our client application and I can specify that I want to call the get DB method on API passing an empty string for the title argument and then use a reference to the database variable which I created up here to catch the result of calling this function and now we can just print it out like we were doing before so that we can actually see what is inside of the database just an FYI - you guys I kind of messed up here I messed up in that I forgot to export both of the fields for the item structure and when you run the RPC client it will just get back no data you also have to do the same thing inside of the client structure so both of these structures need to be completely exported so just make sure that everything is capital in your structure now we can go ahead and run our server so you can see here it's serving on port 40 40 and then if I come into another terminal and go into the client folder I can go ahead and run the main dico file in there and the result of running the client is to actually get back the database as you can see here we get back all the items that we created in our client we get our first a first item second a second item in 33rd item and they're all now inside of the database on our server as well and actually to further demonstrate this point if I go ahead and run the client again you'll see that these items will actually double up and as you can see now we have six items inside of our quote unquote database of course if you were using a proper database you would have some kind of way to replace items so you wouldn't really have to worry about this and you probably wouldn't be directly passing items in in this way at the very least now we also want to activate the other methods that we created so that we can edit and delete and read our various items from our database so let's go ahead and call to our Edit item method now currently with our Edit item method the way it's set up we can't actually change the title of an item that's already in the database because we go and we search through the database for this particular item so when we go to call this item from our client we can only just change the body so in this case what I'm going to do is modify the second item and just have it say a new second item rather than a second item then after we edit the item will call API delete item and we want to delete our see item so the third item and again we'll get the database and then we'll print out the database and then finally we'll call our API get item by name and we'll pass in the string first so it will go and get the first item and then we'll print out that final reply so let's go ahead and spin up this server and then we can go and run the client and as you can see here we have our initial database which has the three initial items in it then we have our second database which has the edited second item inside of it and the third item is removed from it and then finally we have the first item which we pulled out of the data base which just says first a first item alright guys well I hope you enjoyed this tutorial if you did feel free to like and subscribe if you have any questions or comments feel free to leave them in the box below and if you just like this video then by all means download it as much as you like have a good night

Original Description

In this tutorial, we take a look at a simple example of using RPC with Golang. Github Repository: https://github.com/tensor-programming/go-basic-rpc Request Form: https://goo.gl/forms/rFjHcZMRJ3bYPEC03 Cloudways Web App Hosting: https://www.cloudways.com/en/?id=507366 Support the Channel and Join Patreon: Patreon: https://www.patreon.com/tensor_programming Dontate: ETH: 0x03247265dd5242605bD2FA3c40fb3b70d9e3D685 Cardano: addr1q9auccwrr9ws8qdyv45f4qwsx76pfmld4zapks89sakq94ay0xmle73y0r8ruwd0zslls4eglf98lghru7ywv56cedysk7ftjt Check out our Twitter: https://twitter.com/TensorProgram Check out our Facebook: https://www.facebook.com/Tensor-Programming-1197847143611799/ Check out our Steemit: https://steemit.com/@tensor
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tensor Programming · Tensor Programming · 0 of 60

← Previous Next →
1 NodeJs, Text editors and IDEs
NodeJs, Text editors and IDEs
Tensor Programming
2 Vanilla JS todo App
Vanilla JS todo App
Tensor Programming
3 Elm Tutorial part 1
Elm Tutorial part 1
Tensor Programming
4 Elm Lang Tutorial, Part 2
Elm Lang Tutorial, Part 2
Tensor Programming
5 Elm Tutorial Part 3
Elm Tutorial Part 3
Tensor Programming
6 Elm Tutorial Part 4 -- Analog Clock App
Elm Tutorial Part 4 -- Analog Clock App
Tensor Programming
7 Elm Tutorial part 5 -- Snake Game
Elm Tutorial part 5 -- Snake Game
Tensor Programming
8 Elm Tutorial part 6 -- Calculator
Elm Tutorial part 6 -- Calculator
Tensor Programming
9 Go Tutorial part 1 -- Hello World and Static File Server
Go Tutorial part 1 -- Hello World and Static File Server
Tensor Programming
10 Go Tutorial part 2 -- Web Crawler
Go Tutorial part 2 -- Web Crawler
Tensor Programming
11 Go Tutorial Part 3 (Web App part 1)
Go Tutorial Part 3 (Web App part 1)
Tensor Programming
12 Go tutorial Part 4 (Web tutorial part 2) - Using templates
Go tutorial Part 4 (Web tutorial part 2) - Using templates
Tensor Programming
13 Go tutorial part 5 (web app part 3)
Go tutorial part 5 (web app part 3)
Tensor Programming
14 Go tutorial part 6 (webapp part 4)
Go tutorial part 6 (webapp part 4)
Tensor Programming
15 Go tutorial part 7 (web app part 5)
Go tutorial part 7 (web app part 5)
Tensor Programming
16 Go tutorial part 8 (Web app part 6)
Go tutorial part 8 (Web app part 6)
Tensor Programming
17 Go tutorial Part 9 (web tutorial part 7)
Go tutorial Part 9 (web tutorial part 7)
Tensor Programming
18 Go tutorial Part 10 (web app part 8)
Go tutorial Part 10 (web app part 8)
Tensor Programming
19 Go tutorial Part 11 (Web app Part 9)
Go tutorial Part 11 (Web app Part 9)
Tensor Programming
20 Go Tutorial Part 12 (Web app Part 10)
Go Tutorial Part 12 (Web app Part 10)
Tensor Programming
21 Go Tutorial Part 13 (Web app Part 11)
Go Tutorial Part 13 (Web app Part 11)
Tensor Programming
22 Looking at Elm 0.18
Looking at Elm 0.18
Tensor Programming
23 Go tutorial Part 14 (Web tutorial part 12)
Go tutorial Part 14 (Web tutorial part 12)
Tensor Programming
24 Go tutorial Part 15 (Web tutorial part 13)
Go tutorial Part 15 (Web tutorial part 13)
Tensor Programming
25 Go tutorial part 16 (web app part 14)
Go tutorial part 16 (web app part 14)
Tensor Programming
26 Elm Tutorial Part 7 (SPA part 1)
Elm Tutorial Part 7 (SPA part 1)
Tensor Programming
27 Elm Tutorial Part 8 (SPA Part 2)
Elm Tutorial Part 8 (SPA Part 2)
Tensor Programming
28 Electron Elm Tutorial
Electron Elm Tutorial
Tensor Programming
29 Go tutorial part 17 (web app part 15)
Go tutorial part 17 (web app part 15)
Tensor Programming
30 Up and Coming Programming Languages and Technologies for 2017
Up and Coming Programming Languages and Technologies for 2017
Tensor Programming
31 elixir tutorial part 1
elixir tutorial part 1
Tensor Programming
32 elixir tutorial part 2
elixir tutorial part 2
Tensor Programming
33 Elixir tutorial Part 3 (GenServer and Supervisor)
Elixir tutorial Part 3 (GenServer and Supervisor)
Tensor Programming
34 Elixir Tutorial Part 4 (GenStage)
Elixir Tutorial Part 4 (GenStage)
Tensor Programming
35 Elixir Tutorial Part 5 (Plug and Cowboy)
Elixir Tutorial Part 5 (Plug and Cowboy)
Tensor Programming
36 Phoenix Framework Tutorial Part 1 (elixir part 6)
Phoenix Framework Tutorial Part 1 (elixir part 6)
Tensor Programming
37 Phoenix Framework Tutorial Part 2  (elixir part 7)
Phoenix Framework Tutorial Part 2 (elixir part 7)
Tensor Programming
38 Phoenix Framework Tutorial Part 3 (elixir part 8)
Phoenix Framework Tutorial Part 3 (elixir part 8)
Tensor Programming
39 A Intro to Clojure and Clojure Syntax
A Intro to Clojure and Clojure Syntax
Tensor Programming
40 An Update about the channel
An Update about the channel
Tensor Programming
41 Intro to Rustlang (Setup and Primitives)
Intro to Rustlang (Setup and Primitives)
Tensor Programming
42 Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Intro to Rustlang (Strings, Tuples, Arrays, Slices and Pretty Printing)
Tensor Programming
43 Intro to Rustlang (Ownership and Borrowing)
Intro to Rustlang (Ownership and Borrowing)
Tensor Programming
44 Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Intro to Rustlang (Structs, Methods, Functions, Related Functions and the Display/Debug Traits)
Tensor Programming
45 Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Intro to Rustlang (Control Flow, Conditionals and Pattern Matching)
Tensor Programming
46 Intro to RustLang (Enums and Options)
Intro to RustLang (Enums and Options)
Tensor Programming
47 Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Intro to Rustlang (Vectors, HashMaps, Casting, If-Let, While-Let, and the Result Enum)
Tensor Programming
48 Rustlang Project: Snake Game
Rustlang Project: Snake Game
Tensor Programming
49 Intro to Rustlang (Traits and Generic Types)
Intro to Rustlang (Traits and Generic Types)
Tensor Programming
50 Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Intro to Rust-lang (Closures, the Box Pointer and Iterators)
Tensor Programming
51 Intro to Rust-lang (Modules and Lifetimes)
Intro to Rust-lang (Modules and Lifetimes)
Tensor Programming
52 Intro to Rust-lang (Macros and Metaprogramming)
Intro to Rust-lang (Macros and Metaprogramming)
Tensor Programming
53 Intro to Rust-lang (Error Handling)
Intro to Rust-lang (Error Handling)
Tensor Programming
54 Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Intro to Rust-lang (Concurrency, Threads, Channels, Mutex and Arc)
Tensor Programming
55 Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Intro to Rust-lang (Tests, Attributes, Configuration and Conditional compilation)
Tensor Programming
56 Rustlang Project: Port Sniffer CLI
Rustlang Project: Port Sniffer CLI
Tensor Programming
57 Rustlang Project: Chat Application
Rustlang Project: Chat Application
Tensor Programming
58 Rustlang Project: CLI Toy Blockchain
Rustlang Project: CLI Toy Blockchain
Tensor Programming
59 Intro to Rust-lang (Setting up a Development Environment)
Intro to Rust-lang (Setting up a Development Environment)
Tensor Programming
60 Intro to Rust-lang (Building a Web API with Iron)
Intro to Rust-lang (Building a Web API with Iron)
Tensor Programming

This video teaches how to build a basic RPC server and client using Go, covering RPC library, net RPC library, and Go tools, with a focus on CRUD application and API design. The tutorial provides a step-by-step guide on how to implement RPC methods, register an HTTP handler, and create a client application to access the RPC server.

Key Takeaways
  1. Create a new API type to register its methods for remote calls
  2. Register the API type using the RPC register method
  3. Use the net.Listen function to open a TCP connection on port 4040
  4. Create an HTTP handler to handle incoming requests
  5. Use the reply pointer to return data to the caller from each method
  6. Create a client application to access the RPC
  7. Define the item type and create a makeshift database
  8. Execute methods from the client using the client call method
  9. Create a function to read the database through the client
💡 To make RPC work correctly, both structures need to be completely exported with everything in capital letters, and the server needs to serve on a specific port.

Related Reads

📰
7 AI Notion Workflows That Actually Run in 2026 (Honest Comparison, incl. Easlo & Thomas Frank)
Learn 7 effective AI Notion workflows for 2026, including Easlo and Thomas Frank's systems, to boost productivity
Dev.to AI
📰
ChatGPT Plus and Claude Pro reject your card? It is probably the billing country, not the card
Learn why ChatGPT Plus and Claude Pro may reject your card due to billing country issues, not the card itself, and how to resolve it
Dev.to · Tung@fizen
📰
Lost in the Cheese Aisle? Here’s How AI Can Identify Any Cheese From a Photo
Learn how AI can identify cheese from a photo, revolutionizing food recognition
Medium · Startup
📰
Three ranking currencies and zero overlap: what 2025 Juejin AI roundups actually disagree about
Discover the three incompatible ranking currencies in Juejin's 2025 AI tool roundups and their implications for AI tool evaluation
Dev.to AI
Up next
how i use a.i. to create viral UGC influencer facebook ads.
Austin Rabin
Watch →