Path: blob/main/docs/versioned_docs/version-v28/03-clients/01-go-client.md
1700 views
------A client in the Go programming language
In this tutorial, we will show you how to create a standalone Go program that serves as a client for a blockchain. We will use the Ignite CLI to set up a standard blockchain. To communicate with the blockchain, we will utilize the cosmosclient package, which provides an easy-to-use interface for interacting with the blockchain. You will learn how to use the cosmosclient package to send transactions and query the blockchain. By the end of this tutorial, you will have a good understanding of how to build a client for a blockchain using Go and the cosmosclient package.
Create a blockchain
To create a blockchain using the Ignite CLI, use the following command:
This will create a new Cosmos SDK blockchain called "blog".
Once the blockchain has been created, you can generate code for a "blog" model that will enable you to perform create, read, update, and delete (CRUD) operations on blog posts. To do this, you can use the following command:
This will generate the necessary code for the "blog" model, including functions for creating, reading, updating, and deleting blog posts. With this code in place, you can now use your blockchain to perform CRUD operations on blog posts. You can use the generated code to create new blog posts, retrieve existing ones, update their content, and delete them as needed. This will give you a fully functional Cosmos SDK blockchain with the ability to manage blog posts.
Start your blockchain node with the following command:
Creating a blockchain client
Create a new directory called blogclient on the same level as blog directory. As the name suggests, blogclient will contain a standalone Go program that acts as a client to your blog blockchain.
This command will create a new directory called blogclient in your current location. If you type ls in your terminal window, you should see both the blog and blogclient directories listed.
To initialize a new Go package inside the blogclient directory, you can use the following command:
This will create a go.mod file in the blogclient directory, which contains information about the package and the Go version being used.
To import dependencies for your package, you can add the following code to the go.mod file:
Your package will import two dependencies:
blog, which containstypesof messages and a query clientignitefor thecosmosclientpackage
The replace directive uses the package from the local blog directory and is specified as a relative path to the blogclient directory.
Cosmos SDK uses a custom version of the protobuf package, so use the replace directive to specify the correct dependency.
Finally, install dependencies for your blogclient:
Main logic of the client in main.go
Create a main.go file inside the blogclient directory and add the following code:
The code above creates a standalone Go program that acts as a client to the blog blockchain. It begins by importing the required packages, including the general purpose Cosmos blockchain client and the types package of the blog blockchain.
In the main function, the code creates a Cosmos client instance and sets the address prefix to "cosmos". It then retrieves an account named "alice" from the keyring and gets the address of the account using the address prefix.
Next, the code defines a message to create a blog post with the title "Hello!" and body "This is the first post". It then broadcasts a transaction from the account "alice" with the message to create the post, and stores the response in the variable txResp.
The code then instantiates a query client for the blog blockchain and uses it to query the blockchain to retrieve all the posts. It stores the response in the variable queryResp and prints it to the console.
Finally, the code prints the response from broadcasting the transaction to the console. This allows the user to see the results of creating and querying a blog post on the blog blockchain using the client.
To find out more about the cosmosclient package, you can refer to the Go package documentation for cosmosclient. This documentation provides information on how to use the Client type with Options and KeyringBackend.
Run the blockchain and the client
Make sure your blog blockchain is still running with ignite chain serve.
Run the blockchain client:
If the command is successful, the results of running the command will be printed to the terminal. The output may include some warnings, which can be ignored.
As you can see the client has successfully broadcasted a transaction and queried the chain for blog posts.
Please note, that some values in the output on your terminal (like transaction hash and block height) might be different from the output above.
You can confirm the new post with using the blogd q blog list-post command:
Great job! You have successfully completed the process of creating a Go client for your Cosmos SDK blockchain, submitting a transaction, and querying the chain.