<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Serhat Karabulut on Medium]]></title>
        <description><![CDATA[Stories by Serhat Karabulut on Medium]]></description>
        <link>https://medium.com/@3n0ugh?source=rss-fcb0b0fcd9d4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*sNs1-uzVuMV_lgCpSNcs-A.png</url>
            <title>Stories by Serhat Karabulut on Medium</title>
            <link>https://medium.com/@3n0ugh?source=rss-fcb0b0fcd9d4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:02:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@3n0ugh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Test gRPC Servers in Go]]></title>
            <link>https://medium.com/@3n0ugh/how-to-test-grpc-servers-in-go-ba90fe365a18?source=rss-fcb0b0fcd9d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba90fe365a18</guid>
            <category><![CDATA[how-to-test-grpc]]></category>
            <category><![CDATA[test-grpc-server-go]]></category>
            <category><![CDATA[testing-grpc-server-in-go]]></category>
            <category><![CDATA[test-grpc-server-golang]]></category>
            <dc:creator><![CDATA[Serhat Karabulut]]></dc:creator>
            <pubDate>Sun, 24 Apr 2022 23:36:56 GMT</pubDate>
            <atom:updated>2022-04-25T07:34:17.166Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iH4RWGR27viq1OGOs3ykYA.jpeg" /><figcaption><a href="https://en.wikipedia.org/wiki/Franz_Sedlacek">Franz Sedlacek</a> — Traum 1932</figcaption></figure><p>In this article, I will show how to create a gRPC server and then write the tests for it.</p><blockquote>I don’t go into great detail on how to create a gRPC server, so if you want a more detailed tutorial, you can check out the grpc.io <a href="https://grpc.io/docs/languages/go/basics/">tutorial</a>.</blockquote><p>Our example is simple. We have a telephone service which has capable of doing the followings:</p><ul><li><strong>Search contacts:</strong> We can get contact information by sending a request to the GetContact endpoint with a phone number.</li><li><strong>List contacts:</strong> We can get a list of contact information by sending a request to the List Contacts endpoint.</li><li><strong>Record Call History: </strong>We can record the callers and then see them by sending a request to the RecordCallHistory endpoint.</li><li><strong>Send Message: </strong>We can send a message by sending a text request to the SendMessage endpoint.</li></ul><p>Let’s begin defining our gRPC server with the proto file. If you want to move on to the testing part, the code repository is <a href="https://github.com/3n0ugh/grpc-test-sample">here</a>.</p><h3>Defining Proto File</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9758622e663dffbadb067aa7efc1103/href">https://medium.com/media/e9758622e663dffbadb067aa7efc1103/href</a></iframe><p>Let’s quickly go through the rpc definitions:</p><ul><li><strong>Simple RPC:</strong> The client sends a request to the server using the stub and waits for a response to come back like a normal function call.</li><li><strong>Server-Side Streaming RPC:</strong> The client sends a request to the server and gets back a stream to read a sequence of messages. The client reads from the returned stream until there are no more messages.</li><li><strong>Client-Side Streaming RPC:</strong> The client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them and return its response.</li><li><strong>Bidirectional Streaming RPC:</strong> Both sides send a sequence of messages using a read-write stream. The two streams run independently, so clients and servers can read and write in any order.</li></ul><p>Generate client and server code from proto file with this command:</p><pre>protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. \<br>--go-grpc_opt=paths=source_relative pb/telephone.proto</pre><h3>Implementing Telephone Service</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/793886ef4f4b039032e4a154678b31a9/href">https://medium.com/media/793886ef4f4b039032e4a154678b31a9/href</a></iframe><blockquote>🎉 Successfully, we created a gRPC server. Let’s start to write the test for it.</blockquote><h3>Testing gRPC Server</h3><p>gRPC and HTTP server testing are so similar to each other in Go. We will use <a href="https://pkg.go.dev/google.golang.org/grpc/test/bufconn">the bufconn</a> package to start the gRPC server so that tests are interacting with actual network behaviors.</p><ul><li>First, let’s define a server function that can start the gRPC server:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f799f83e869398df5fa09a68eb07f8e/href">https://medium.com/media/6f799f83e869398df5fa09a68eb07f8e/href</a></iframe><ul><li><strong>Simple RPC Testing:</strong></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/885ee5e7fe974e39347e9f4531bee2d4/href">https://medium.com/media/885ee5e7fe974e39347e9f4531bee2d4/href</a></iframe><ul><li><strong>Server-Side Streaming RPC Testing:</strong></li></ul><p>We will use slice and for loop to simulate server-side streaming. In the for loop, we will get replies using out.Recv() function and store them at outs slice.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0613fbca2497b4b251272e119ae8c11a/href">https://medium.com/media/0613fbca2497b4b251272e119ae8c11a/href</a></iframe><ul><li><strong>Client-Side Streaming RPC Testing:</strong></li></ul><p>We will use for loop to simulate client-side streaming. In the for loop, we will send requests to the RecordCallHistory client using outClient.Send(). Once all requests have been sent, we will close the sending.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d93ae8c433c90290b1d51aa06525d331/href">https://medium.com/media/d93ae8c433c90290b1d51aa06525d331/href</a></iframe><ul><li><strong>Bidirectional Streaming RPC Testing:</strong></li></ul><p>This time, we will use the same techniques we used in both client-side and server-side rendering. First, we will send requests in the for loop using outClient.Send(). And then, we will get replies using outClient.Recv() and store them at outs slice.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa9a5df7b38535252836d82db2850bfb/href">https://medium.com/media/aa9a5df7b38535252836d82db2850bfb/href</a></iframe><p>We can run tests with this command:</p><pre>go test ./... -v</pre><p>Here is the output:</p><figure><img alt="Terminal output" src="https://cdn-images-1.medium.com/max/1024/1*u_TlkUZnOgzSWN1R0CpPCw.png" /></figure><blockquote>🎉 Congratulations, we are done! 🎉</blockquote><blockquote>✨ If you want to see more examples about testing gRPC servers, check this <a href="https://github.com/3n0ugh/kalenderium">repository</a>. It has two gRPC service test examples: <a href="https://github.com/3n0ugh/kalenderium/blob/main/pkg/calendar/calendar_test.go">calendar</a> and <a href="https://github.com/3n0ugh/kalenderium/blob/main/pkg/account/account_test.go">account</a>.</blockquote><p>To check the code, here is the <a href="https://github.com/3n0ugh/grpc-test-sample">repository</a>.</p><blockquote>🧙🏻 — — — — Thanks For Reading — — — — 🧙🏻</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba90fe365a18" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test HTTP Handlers in Go]]></title>
            <link>https://medium.com/@3n0ugh/test-http-handlers-in-go-97dc01d05c3e?source=rss-fcb0b0fcd9d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/97dc01d05c3e</guid>
            <dc:creator><![CDATA[Serhat Karabulut]]></dc:creator>
            <pubDate>Wed, 13 Apr 2022 21:36:28 GMT</pubDate>
            <atom:updated>2022-04-13T21:38:04.826Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/477/1*XiZV8E-PDkEFSYOAQxpZ0g.jpeg" /><figcaption>Richard Alexandrovich Bergholz</figcaption></figure><p>In this article, I want to show how to write a test for HTTP handlers that interact with the database using the httptest package.</p><p>We will look at how to inject the necessary dependencies and mock the database calls.</p><h3>Go httptest Package</h3><p><a href="https://pkg.go.dev/net/http/httptest">https://pkg.go.dev/net/http/httptest</a></p><p>Go has an amazing testing package in its standard library httptest package. It provides a small set of structures and functions for HTTP testing.</p><p>Let’s have a look at how you can use the httptest package and build better end-to-end and http tests for your Go handlers.</p><h3>A Basic Handler</h3><p>First, creates a directory and initialize the go.mod file:</p><pre>mkdir test-handlers-go</pre><pre>go mod init </pre><p>Here is the basic handler that returns an HTTP 200 (OK) status code and a ‘hello world!’ message. This is our handler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee996a146464024f51d163704214630b/href">https://medium.com/media/ee996a146464024f51d163704214630b/href</a></iframe><p>and the test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6cc2ecb97c20240c1f6473e57353199c/href">https://medium.com/media/6cc2ecb97c20240c1f6473e57353199c/href</a></iframe><p>We just construct a request, a response, and pass them as a parameter to our handler function. Then check the status code and response body.</p><blockquote>As you can see, Go’s <a href="https://golang.org/pkg/testing/">testing</a> and <a href="https://golang.org/pkg/net/http/httptest/">httptest</a> packages make testing our handlers extremely simple.</blockquote><p>I hope you are now clear about the usage httptest package to test handlers. That’s the main idea. But more clarifying, I give an example on CRUD API.</p><h3>Simple CRUD API</h3><p>I created a simple CRUD API that has these endpoints:</p><ul><li>Send a POST request to /v1/book to create a new book</li><li>Send GET request to /v1/book gets all book</li><li>Send DELETE request to /v1/book/:id delete book according to the given id</li><li>Send PUT request to /v1/book/:id update book information</li></ul><blockquote>I don’t show how to build the API, you can check <a href="https://github.com/3n0ugh/simple-crud-api">here</a> the API. We will just write a test for it.</blockquote><p>Let’s start with mocking the database calls:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8ad979eda737552a3400f24fb04ea1a/href">https://medium.com/media/b8ad979eda737552a3400f24fb04ea1a/href</a></iframe><p>Let’s define structs for test cases:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0dbb46b64bfa9b556f87b1cec3b7803e/href">https://medium.com/media/0dbb46b64bfa9b556f87b1cec3b7803e/href</a></iframe><p><strong>Test Handler (POST Method)</strong></p><p>Now, we can write a test for AddBookHandler:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/255c73e35d9f7d20b80f15a62886f18d/href">https://medium.com/media/255c73e35d9f7d20b80f15a62886f18d/href</a></iframe><ul><li>Inject the database calls that we mocked before.</li><li>Define test cases.</li><li>And run the test cases.</li></ul><p><strong>Test Handler (Delete Method + Parameter)</strong></p><p>When sending a request to the DeleteBook endpoint, we need an id parameter. Let’s look at how we can add parameters while testing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aa7fb6a5b13e9b99956fd66f167be1b/href">https://medium.com/media/6aa7fb6a5b13e9b99956fd66f167be1b/href</a></iframe><blockquote>The rest of the handler tests are available on the <a href="https://github.com/3n0ugh/simple-crud-api">GitHub</a> repository.</blockquote><blockquote>→ Have a Good Day ←</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97dc01d05c3e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating CLI In Go With Cobra]]></title>
            <link>https://medium.com/@3n0ugh/creating-cli-in-go-with-cobra-d6f83dcbab1c?source=rss-fcb0b0fcd9d4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6f83dcbab1c</guid>
            <category><![CDATA[cli]]></category>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[read-file]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[cobra]]></category>
            <dc:creator><![CDATA[Serhat Karabulut]]></dc:creator>
            <pubDate>Mon, 18 Oct 2021 12:23:18 GMT</pubDate>
            <atom:updated>2021-10-18T12:23:18.427Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_bQpMtwgIZZVUjkD491USQ.jpeg" /></figure><p>We will create a simple cli that will be capable of doing the followings:</p><p>Calculates:</p><ul><li>word count,</li><li>number of letter,</li><li>number of vowels/consonants,</li><li>the number of punctuation marks/spaces.</li></ul><p>Before we dive into building the command line application, let’s take a quick peek at Command-Line.</p><h3>What Is The CLI ?</h3><p>Command-line interface (CLI) is a text-based interface that is used to operate software and operating systems while allowing the user to respond to visual prompts by typing single commands into the interface and receiving a reply in the same way.</p><p>In Mac or Linux, it is known by the name terminal.</p><h3>Cobra Concepts</h3><p><a href="https://github.com/spf13/cobra">GitHub - spf13/cobra: A Commander for modern Go CLI interactions</a></p><p>Cobra is built on a structure of commands, arguments &amp; flags.</p><ul><li>Commands represent actions</li><li>Args are things</li><li>Flags are modifiers for those actions</li></ul><p>The basic structure will be like a simple sentence:</p><pre>appname command args — flags<br> or <br>appname command — flags args</pre><p>For Example:</p><pre>git commit -m &quot;message&quot;<br>npm install nodemon --save-dev</pre><h3>Let’s Start To Build CLI Project</h3><p>First, create a directory. I created the ReCo directory.</p><pre>mkdir ReCo</pre><p>Move in ReCo and initialize the go.mod file with the following command:</p><pre>go mod init ReCo</pre><p>If you didn’t install the cobra library, you can install it using the below command.</p><pre>go get -u github.com/spf13/cobra/cobra</pre><p>Initialize the cli scaffolding for the project using the `cobra init` command.</p><pre>cobra init --pkg-name ReCo</pre><p>Now we have a file structure like that:</p><pre>ReCo/<br>    cmd/<br>      root.go<br>    main.go<br>    go.mod<br>    go.sum</pre><p>The main.go is the entry point of the cli. Inside the main.go it is calling the execute function of the cmd/root.go.</p><p>Let’s examine the root.go.</p><h4>root.go</h4><p>Root command is the base command of any cli.</p><pre>For example :<br>  go get URL <br>  — go is the root command here, <br>  - the get is the child command of the go. </pre><p>In the root.go, it is initiating the rootCmd struct variable with the cobra command. All the other command in the cli will be a child of the rootCmd.</p><p>If you want to add a function into the rootCmd just open the root.go then uncomment the `<strong>Run : func(cmd *cobra.Command, args []string) {},</strong>` and write the function inside the curly braces.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a52cb45090801b29ad06dc24fd4c9fbd/href">https://medium.com/media/a52cb45090801b29ad06dc24fd4c9fbd/href</a></iframe><blockquote>You can clear the comments from the code.</blockquote><p>Open the terminal inside the project directory and build it.</p><pre>go install</pre><p>This command will generate the binary or executable file of the project in the <br> $GOPATH/bin folder.</p><p>Now run `ReCo` in the terminal. As it is saved in the bin folder, you don’t have to set the environment variable for this.</p><blockquote>If you got an error that says “command not found” open the terminal rc (runcom) file (for zsh terminal it is .zshrc) and add this line:</blockquote><blockquote>export GOBIN=$GOPATH/bin<br> export PATH=$PATH:$GOBIN</blockquote><h4>init</h4><p>This is the first function that gets called whenever a package initializes in the Go. The cobra.OnInitilialize(funcDeclarations) append the user-defined functions in the command’s initialization. Whenever the command is run or called, it will first execute all the functions in the command’s initialization then, it will run the execute method. This initialization can be used in loading the configuration file or can be used as a constructor. It all depends on your use case and your creativity.<br> <br> In the root.go, the command is the rootCmd.</p><p>The cobra.OnInitialize(initConfig) append the initConfig function declaration<br> in the rootCmd’s initialization. So, whenever the rootCmd’s execute method<br> ( RUN: func) run, rootCmd will first run the initConfig function. Once the execution of the initialized functions is over, it will run the RUN: func execution method of rootCmd.</p><p>To visualize it, add a print message in the initConfig function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd99102a5bf99b3faec3343bd5197daf/href">https://medium.com/media/cd99102a5bf99b3faec3343bd5197daf/href</a></iframe><p>Then rebuild the binary.</p><pre>go install</pre><p>Run ReCo.</p><pre>ReCo</pre><p>You can see that ‘<em>initConfig</em>’ run first and later ‘<em>Hello, World!</em>’ .</p><p>To understand the complete flow of cli, add messages inside the init<br> function and the main function in the main.go.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b14c068807aa36affb0c74f46889346/href">https://medium.com/media/5b14c068807aa36affb0c74f46889346/href</a></iframe><p>Rebuild the binary and run the command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ld5Z1s_iclFpGwXUnP7drA.png" /></figure><p>I hope you are now clear about the cli command flow.</p><p>The last thing in the init function is flags.</p><p>Flags are like modifiers to the command.</p><p>There are 2 types of flags Persistent Flags and Local Flags.</p><ul><li><strong>Persistent Flags:</strong> A flag can be persistent, meaning that this flag will be available to the command it’s assigned to as well as every command under that command. For global flags, assign a flag as a persistent flag on the root.</li><li><strong>Local Flags:</strong> A flag can also be assigned locally, which will only apply to that specific command.</li></ul><h4>initConfig</h4><p>This function sets the configuration path in the home directory. The config filename is .ReCo. It will use the configuration file if it exists.</p><p>And this is the last function to explain the root.go and main.go.<br>It’s time to add some commands in our cli.</p><p>Now we need to add a new command. We will add the new command with following:</p><pre>cobra add count</pre><p>Now the count.go file is adding into cmd directory.Open the count.go file. It is similar to the root.go file.<br> <br> First, an addCmd struct variable is declared of type *cobra.Command.The *cobra.Command has RUN which is a function and takes the pointer of *cobra.Command and a slice of string.</p><p>Then it is initialized in the init function. In the init, it is added to the <br> rootCmd. We can understand it as addCmd is the sub-command of the rootCmd.</p><p>Now, install the ReCo and run it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sL6Ao4LPBHy0G0s9ucTtJA.png" /></figure><p>Count command is working fine. It’s time to modify.</p><p>Creating a new directory into the ReCo directory.</p><pre>mkdir counters</pre><p>And adding the word-count.go file into the counters directory</p><pre>touch ./counters/word-count.go</pre><p>Let’s start to code our program.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2042d86c9f54a6a5c86e21683a3018f/href">https://medium.com/media/e2042d86c9f54a6a5c86e21683a3018f/href</a></iframe><p>First, we need to open the file and then read it. We are reading the file word by word using the bufio package. Then, we use atomic counters to count how many words are in the file. And return the word count.</p><p>Now we can back to count.go file and importing our program into it. First, we introduce the word flag into the init function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93967c812156afa826a9122b06eb958d/href">https://medium.com/media/93967c812156afa826a9122b06eb958d/href</a></iframe><p>And time to import word-count.go file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6aa23fa9c46a2f185820ed8a04eb18f/href">https://medium.com/media/a6aa23fa9c46a2f185820ed8a04eb18f/href</a></iframe><p>We are adding our program into the Run Method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27245ec0e8b4423a469d033c7498bbe5/href">https://medium.com/media/27245ec0e8b4423a469d033c7498bbe5/href</a></iframe><ul><li>We are checking args because we just need the one args what is the filename.</li><li>We handle the error caused by the open file.</li><li>If the word flag is called, we call the CountWord function and then print the word count.</li></ul><p>Save all changes and install the ReCo.</p><pre>go install</pre><p>And run. I have a test.txt so, I am writing test.txt instead of the filename.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZC4Ork_DpsVoNR70CwFg7A.png" /></figure><blockquote>Yes, the test.txt file is a little bit bigger 🧙🏻.</blockquote><blockquote>🎉 Congratulations, we are created cli in go with cobra.🎉If you want to continue to improve this cli, keep reading.</blockquote><p><strong>Adding Some Commands</strong></p><p>Now you learn the basics of cli so, I am not explain everything.</p><p>Let’s start to code.</p><p>Adding other-count.go file into the counters directory.</p><pre>.touch ./counters/other-count.go</pre><p>We want to count letters, vowels, consonants, spaces, and punctuation marks in this file.</p><p>First, we create a function named by CountOthers and open the file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90da6dfe78bce6ae8a6f0a9057f0b993/href">https://medium.com/media/90da6dfe78bce6ae8a6f0a9057f0b993/href</a></iframe><p>we declare the atomic counter variables into the CountOthers Function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76d5d9c8e1e5354b3f5dd3e1209277bf/href">https://medium.com/media/76d5d9c8e1e5354b3f5dd3e1209277bf/href</a></iframe><p>And declaring two channels, because we are using the goroutines this time and we need a string channel to send lines and a bool channel to control the process end or not.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9de422159fffd8d4a6f65aafeb1aaa9c/href">https://medium.com/media/9de422159fffd8d4a6f65aafeb1aaa9c/href</a></iframe><p>Declaring a goroutine with an anonymous function. We are reading files line by line in this goroutine and sending lines with lines channel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/039288612124368df398f20a4ddc5f36/href">https://medium.com/media/039288612124368df398f20a4ddc5f36/href</a></iframe><p>Now create a new function named by countQuery. (We are counting things here.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aa1f9c5eb30c103b6c5c52df1fad983/href">https://medium.com/media/7aa1f9c5eb30c103b6c5c52df1fad983/href</a></iframe><ul><li>In this function, we received the lines from lines channel with first for range and second for range on strings iterates over Unicode code points.</li><li>Then checking it is vowel or consonant or punctuation mark or space or digit with switch case.</li><li>Lastly, we sent true value on the done channel to say “Our work is done.”.</li></ul><p>Now, we return the CountOthers function and continue to code.</p><p>Adding the second goroutine with the countQuery function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d1f80241dd39d2d4ed56e3cc9f39a4a/href">https://medium.com/media/2d1f80241dd39d2d4ed56e3cc9f39a4a/href</a></iframe><p>And to wait until the countQuery end adding ‘<strong><em>&lt;-done</em></strong>’ and close the done channel:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c85605ce22d44292ad7ffe9b4600b511/href">https://medium.com/media/c85605ce22d44292ad7ffe9b4600b511/href</a></iframe><p>Now we return our counts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e17c386c5f80b18f4e34f5d7493a5aa6/href">https://medium.com/media/e17c386c5f80b18f4e34f5d7493a5aa6/href</a></iframe><p>And our second program is ready, time to adding into count.go file.</p><p>We are adding flags as well:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/552669a617e162fcabd445fe117580bb/href">https://medium.com/media/552669a617e162fcabd445fe117580bb/href</a></iframe><p>Then importing into Run Method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fc1d647b726f0a21119371a94790ebd/href">https://medium.com/media/8fc1d647b726f0a21119371a94790ebd/href</a></iframe><blockquote>Lastly, I want to see process time 😁.</blockquote><p>Adding time flag:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4da5d14ca04908d4c54b586f1a09844e/href">https://medium.com/media/4da5d14ca04908d4c54b586f1a09844e/href</a></iframe><p>And adding into Run Method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4566bfdbf1dba8174767fa511bd53888/href">https://medium.com/media/4566bfdbf1dba8174767fa511bd53888/href</a></iframe><p>Save changes, install and run the ReCo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BD4cukbg6ggWjs1ZRzNoZQ.png" /></figure><p><strong>To check the full version of the cli, here is the </strong><a href="https://github.com/3n0ugh/ReCo"><strong>code</strong></a><strong>.</strong></p><blockquote>🧙🏻 — — — — Thanks For Reading — — — — 🧙🏻</blockquote><blockquote><strong>source : </strong><a href="https://en.wikipedia.org/wiki/Command-line_interface"><strong>https://en.wikipedia.org/wiki/Command-line_interface</strong></a><strong><br>source : </strong><a href="https://pkg.go.dev/github.com/spf13/cobra"><strong>https://pkg.go.dev/github.com/spf13/cobra</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6f83dcbab1c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>