<rss version="2.0">
  <channel>
    <title>Meet Gor - Tag: go</title>
    <link>https://www.meetgor.com</link>
    <description>Posts tagged with go</description>
    <language>en-us</language>
    <pubDate>Sat, 10 Oct 2026 09:08:39 UTC</pubDate>
    <item>
      <title>Crossposter Agent</title>
      <link>https://www.meetgor.com/projects/crossposter-agent</link>
      <description>An AI agent for posting your thoughts to any platform</description>
      <pubDate>Sun, 15 Mar 2026 00:00:00 UTC</pubDate>
      <content>&#xA;An AI agent for posting your thoughts to any platform.&#xA;Built to automate multi-platform publishing while keeping the workflow lightweight.&#xA;</content>
      <type>projects</type>
    </item>
    <item>
      <title>GCP Log Explorer TUI</title>
      <link>https://www.meetgor.com/projects/gcp-log-explorer-tui</link>
      <description>TUI for GCP&#39;s Log Explorer UI</description>
      <pubDate>Sun, 15 Mar 2026 00:00:00 UTC</pubDate>
      <content>&#xA;TUI for GCP&#39;s Log Explorer UI.&#xA;Built to make log exploration faster from the terminal without relying on the web UI.&#xA;</content>
      <type>projects</type>
    </item>
    <item>
      <title>Printf Debugging in Go with Q</title>
      <link>https://www.meetgor.com/links/q-but-for-go-printf-debugging</link>
      <description>This is cool, we make logging a mess. For logs we need to have separate scripts to get relevant data. How much chaos it can be.</description>
      <pubDate>Thu, 08 Jan 2026 00:00:00 UTC</pubDate>
      <content>This is cool, we make logging a mess. For logs we need to have separate scripts to get relevant data. How much chaos it can be.</content>
      <type>links</type>
    </item>
    <item>
      <title>Building Golang from Source v1.23 and Above</title>
      <link>https://www.meetgor.com/til/golang-build-from-source-1-24-above</link>
      <description>Exploring one of the way to install and build golang from source for version 1.23 and above.</description>
      <pubDate>Tue, 19 Nov 2024 00:00:00 UTC</pubDate>
      <content>## Introduction&#xA;&#xA;Are you excited to try out the latest golang version, or test out your changes (cooking some serious stuff?), or install some random golang version? Then let’s explore one of the easiest ways to install golang on your system (Linux).&#xA;&#xA;## Building Go from source 1.23+&#xA;&#xA;The process for installing and building golang from source is quite simple.&#xA;&#xA;* Clone the repo&#xA;    &#xA;* Build the binary&#xA;    &#xA;* Set Environment Variables&#xA;    &#xA;* Export the binary to the system path&#xA;    &#xA;&#xA;For detailed other steps, you can follow [this guide](https://go.dev/doc/install/source).&#xA;&#xA;### Clone the repo&#xA;&#xA;Just clone the repo from GitHub or Google Git repo.&#xA;&#xA;```bash&#xA;git clone https://github.com/golang/go&#xA;&#xA;OR&#xA;&#xA;git clone https://go.googlesource.com/go&#xA;```&#xA;&#xA;This will install the golang source code required to build the golang binary and the ecosystem (gofmt + standard library + test suite).&#xA;&#xA;Then let’s navigate to the cloned repo and we can build the golang from the source.&#xA;&#xA;### Build it&#xA;&#xA;We need to run the bash script in the folder to build the binary. They `all.bash` can be run to build the binary which will be stored in the `go/bin` folder `go/bin/go` and `go/bin/gofmt` files. These two binaries will be generated and are required in the Golang ecosystem.&#xA;&#xA;```bash&#xA;cd src&#xA;&#xA;./all.bash&#xA;```&#xA;&#xA;Once we have the binaries in the specified folder, we can move ahead to make the environment understand where the actual binary is located.&#xA;&#xA;### Environment Variables&#xA;&#xA;The `GOROOT` and `GOPATH` variables are required for the golang ecosystem to work as expected. The `GOROOT` is set as the path to the actual golang source repository, the cloned repository from which we built the binary. This `GOPATH` is the path where Golang stores the external repositories or modules for use anywhere in the system.&#xA;&#xA;```bash&#xA;export GOROOT=path_to_clone_repo&#xA;&#xA;export GOPATH=$(HOME)/go&#xA;&#xA;export PATH=$PATH:$GOROOT/bin:$GOPATH/bin&#xA;```&#xA;&#xA;The `PATH` environment needs to be updated with the `GOROOT` and `GOPATH` to make the binaries in those paths visible and accessible to the system.&#xA;&#xA;&gt; NOTE: If you are installing the golang from source when you have already a version of golang installed on your system, then you need to make sure you do not mess up the `GOROOT` and `GOPATH`.&#xA;&gt; &#xA;&gt; Those could be juse set with the current shell session, as you do not want this golang version permantely on the system, if you do requrie the newly installed golang version as your default, then you can set this environment variables in your shell config.&#xA;&#xA;Finally, we can now set the binary as something different because we do not want it to clash with the default golang version.&#xA;&#xA;### Run the binary&#xA;&#xA;The binary can be stored in the `/usr/local/bin/` to make any binary available in the system from anywhere. This is not necessary but handy if you are going to use it commonly but don’t need it as the default golang version.&#xA;&#xA;```bash&#xA;# The binary is stored in the &#xA;# path_to_cloned_repo/bin&#xA;# Whatever you want to name the binary&#xA;&#xA;cp bin/go /usr/local/bin/go-dev&#xA;&#xA;OR&#xA;&#xA;cp bin/go /usr/local/bin/go1.24&#xA;```&#xA;&#xA;Once this is done. we can check the installed golang version&#xA;&#xA;```bash&#xA;go1.24 version&#xA;```&#xA;&#xA;With this, you can use it as go1.24 or go-dev as the binary name.&#xA;&#xA;So, that is how we install and build from source any golang version above 1.23.&#xA;&#xA;## Conclusion&#xA;&#xA;For context, I wanted to check out the latest changes in 1.24, so I cloned the repo and after some trial and error of some commands to build the golang version, I was able to do it correctly. So just decided to share it here, hope you found it helpful.&#xA;&#xA;Thank you for reading.&#xA;&#xA;Happy Coding :)</content>
      <type>til</type>
    </item>
    <item>
      <title>Connect LibSQL Database hosted on Turso in a Golang Application</title>
      <link>https://www.meetgor.com/posts/turso-libsql-db-golang</link>
      <description>Introduction Welcome to the new series in Golang, Let&#39;s Go with Turso. In this series, we will learn how to interact with LibSQL databases with Golang. We will</description>
      <pubDate>Mon, 30 Sep 2024 00:00:00 UTC</pubDate>
      <content>## Introduction&#xA;&#xA;Welcome to the new series in Golang, Let&#39;s Go with Turso. In this series, we will learn how to interact with LibSQL databases with Golang. We will connect with a remote/local LibSQL database, create Embedded replicas, set up a local LibSQL database, and so much more as we explore and find out more features of LibSQL.&#xA;&#xA;## Connect a LibSQL database in a Golang application&#xA;&#xA;In this post, we will learn how to connect and query a LibSQL database hosted on Turso/Cloud in a Golang Application using libsql-client package. We will go from setting up golang project, installing turso-cli, creating a database on turso with the cli, connecting to the database with shell, and golang and finally, we can query the database using Golang.&#xA;&#xA;If you want to check out the YouTube video, check this out:&#xA;&#xA;[Conenct LibSQL Database hosted on Turso with Golang](https://www.youtube.com/watch?v=vBrvX0X0phw)&#xA;&#xA;&lt;iframe width=&#34;560&#34; height=&#34;315&#34; src=&#34;https://www.youtube.com/embed/vBrvX0X0phw&#34; frameborder=&#34;0&#34; allowfullscreen&gt;&lt;/iframe&gt;&#xA;&#xA;### Initializing a Golang project&#xA;&#xA;Let&#39;s start with initializing a Golang project.&#xA;&#xA;```bash&#xA;# go mod init &lt;git-provider-domain&gt;/&lt;username&gt;/&lt;project-name&gt;&#xA;# Example&#xA;&#xA;go mod init github.com/mr-destructive/lets-go-with-turso&#xA;&#xA;```&#xA;&#xA;This will initialize the project in the current directory, creating a `go.mod` file with the specification of the Golang version and the packages that we will install and use in this module.&#xA;&#xA;### Installing Turso CLI&#xA;&#xA;```bash&#xA;# Linux/Windows&#xA;curl -sSfL https://get.tur.so/install.sh | bash&#xA;curl -sSfL https://get.tur.so/install.sh | bash&#xA;&#xA;# macOS&#xA;brew install tursodatabase/tap/turso&#xA;&#xA;```&#xA;&#xA;This will install the Turso CLI. To verify that Turso CLI is installed properly, you can run the version command to check the setup.&#xA;&#xA;```&#xA;turso --version&#xA;```&#xA;&#xA;Once it is installed, we can now log in into Turso platform, simply by running the `auth signup` or `auth login` to Register or Log-in.&#xA;&#xA;```&#xA;turso auth signup&#xA;&#xA;# OR&#xA;&#xA;turso auth login&#xA;```&#xA;&#xA;This will redirect to the browser for the OAuth flow, once signed up and logged in, this will allow to interact with the Turso platform with the CLI that we downloaded.&#xA;&#xA;To make sure we are logged in as the correct user, we can run the `auth whoami` command to get the currently logged-in user.&#xA;&#xA;```&#xA;turso auth whoami&#xA;```&#xA;&#xA;This will print the username if you are logged-in. If everything seems correct, we can move ahead with the database creation step.&#xA;&#xA;### Creating a Remote LibSQL Database on Turso&#xA;&#xA;To create a LibSQL database hosted on Turso, we will use the `turso db create` command.&#xA;&#xA;```&#xA;turso db create&#xA;&#xA;# OR&#xA;&#xA;turso db create &lt;name&gt;&#xA;```&#xA;&#xA;This will create a database with the specified name, even if you don&#39;t provide the name, it will give out a random friendly two-word name to your database. It will create a database on the nearest location available from your location.&#xA;&#xA;This command will output the following:&#xA;&#xA;```&#xA;Created database &lt;db-name&gt; at group default in 1.99s.&#xA;&#xA;Start an interactive SQL shell with the following:&#xA;    turso db shell &lt;db-name&gt;&#xA;&#xA;To see information about the database, including a connection URL, run:&#xA;    turso db show &lt;db-name&gt;&#xA;&#xA;To get an authentication token for the database, run:&#xA;    turso db tokens create &lt;db-name&gt;&#xA;```&#xA;&#xA;The next step, it shows to start an interactive shell, to see information about the database, and to generate an authentication token for the database.&#xA;&#xA;We will move to the next part, which would be to create an authentication token for accessing the database from an external application.&#xA;&#xA;### Generating and Storing Authentication Token for LibSQL Database&#xA;&#xA;After we executed the `db create` command, and it created the database on the Turso cloud, there was a command hint for creating a `token` with the command `db tokens create`&#xA;&#xA;So, this command will create a JWT authentication token, that will be used to connect and read/write to the database.&#xA;&#xA;```bash&#xA;turso db tokens create &lt;db-name&gt;&#xA;&#xA;# OR&#xA;&#xA;turso db tokens create &lt;db-name&gt; --read-only&#xA;&#xA;# OR&#xA;&#xA;turso db tokens create &lt;db-name&gt; --expiration 30d&#xA;```&#xA;&#xA;We can use the simple `db tokens create &lt;db-name&gt;` to create an authentication token for the database with (read + write access). You can copy that returned token into a environment variable, or wherever your application can read that token.&#xA;&#xA;This could be stored in the environment variable as follows:&#xA;&#xA;```bash&#xA;export TURSO_AUTH_TOKEN=&#34;&lt;token&gt;&#34;&#xA;```&#xA;&#xA;To make a `read-only` token, we can use the flag `--read-only`. This will be handy, if you only have a database as a local replica, and the only purpose of the database is for querying data.&#xA;This will prevent any write operation on the database.&#xA;&#xA;We can also use the `--expiration` flag that will be used to set the duration of the token. By default the value for expiry is `never`, but that could be a little too risky if you are making a serious application. You can either set it to `7d` which will make the token expire after seven days.&#xA;&#xA;&#xA;Now, we can get the remote database URL and connect to the database. The URL could be obtained by running the command `db show &lt;db-name&gt;`&#xA;&#xA;```&#xA;turso db show &lt;db-name&gt;&#xA;```&#xA;&#xA;This will output the following:&#xA;&#xA;```bash&#xA;Name:           &lt;db-name&gt;&#xA;URL:            libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#xA;ID:             &lt;db-id&gt;   &#xA;Group:          default&#xA;Version:        0.24.22&#xA;Locations:      bom&#xA;Size:           4.1 kB&#xA;Archived:       No&#xA;Bytes Synced:   0 B&#xA;Is Schema:      No&#xA;&#xA;Database Instances:&#xA;NAME        TYPE        LOCATION&#xA;bom         primary     bom&#xA;```&#xA;&#xA;The above output shows the meta-information of the database. This also has the URL hosted on Turso. We can construct the URL using the name of the database and your username as `libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io`, you can set this in an environment variable or in the configuration wherever you can access it from the application.&#xA;&#xA;To set the URL of the database in your application, you can use the `TURSO_DB_URL` environment variable.&#xA;&#xA;```bash&#xA;export TURSO_DATABASE_URL=&#34;libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#34;&#xA;```&#xA;&#xA;So, we have a remote database URL, and the access token configured, these are the two pieces that we will need to connect, read and write to the libsql database.&#xA;&#xA;&#xA;### Installing LibSQL Client for Golang&#xA;&#xA;So, we can install the [libsql-client-go](https://pkg.go.dev/github.com/tursodatabase/libsql-client-go/libsql) package for Golang which will be used as an SDK in Golang to interact with a remote LibSQL database.&#xA;&#xA;```bash&#xA;go get github.com/tursodatabase/libsql-client-go/libsql&#xA;```&#xA;&#xA;This will install the package `libsql` into the golang module. Now, we can use this in our golang application.&#xA;&#xA;### Populating the LibSQL Database&#xA;&#xA;Moving ahead, we have a database, but it doesn&#39;t have data! So let&#39;s create some tables and insert some rows. We can use the `db shell` command to open an interactive SQL shell on a remote LibSQL database.&#xA;&#xA;```bash&#xA;turso db shell libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#xA;```&#xA;&#xA;This will be a default `sqlite3` like a shell, where we can execute SQL commands, like `.schema`, `.mode`, `.tables`, etc.&#xA;&#xA;```bash&#xA;  .dump       Render database content as SQL&#xA;  .help       List of all available commands.&#xA;  .indexes    List indexes in a table or database&#xA;  .mode       Set output mode&#xA;  .quit       Exit this program&#xA;  .read       Execute commands from a file&#xA;  .schema     Show table schemas.&#xA;  .tables     List all existing tables in the database.&#xA;```&#xA;&#xA;And definitely, we can use the normal SQL queries, to read, write and delete data from the database.&#xA;&#xA;#### Creating a Table&#xA;&#xA;First, let&#39;s create a simple table, called `posts` with columns like `id`, `title` as a `VARCHAR(100)`, `description` as a `VARCHAR(255)`, and `content` as `TEXT` which won&#39;t be `NULL`.&#xA;&#xA;```sql&#xA;CREATE TABLE posts&#xA;  (&#xA;     id          INTEGER PRIMARY KEY,&#xA;     title       VARCHAR(100),&#xA;     description VARCHAR(255),&#xA;     content     TEXT NOT NULL&#xA;  ); &#xA;```&#xA;&#xA;This will create a table `posts` on the LibSQL database, yes this will mutate the primary LibSQL database which is hosted on Turso.&#xA;&#xA;#### Inserting Rows&#xA;&#xA;Now, since we have the `posts` table, we will insert some rows into the table.&#xA;&#xA;```bash&#xA;INSERT INTO posts (title, description, content)&#xA;VALUES &#xA;    (&#39;test title&#39;, &#39;test description&#39;, &#39;test content&#39;);&#xA;```&#xA;&#xA;Now, we have some rows populated in the `posts` table. We can add more tables, and rows into the database, as usual, but this is just an example, so I&#39;ll keep it short.&#xA;&#xA;### Connecting to the LibSQL Database&#xA;&#xA;Now, we have something to query from a database, after we connect to the database via the Golang application program.&#xA;&#xA;First, we will grab two pieces to connect to the database.&#xA;&#xA;```bash&#xA;export TURSO_DATABASE_URL=&#34;libsql://&lt;db-name&gt;-&lt;username&gt;.turso.io&#34;&#xA;export TURSO_AUTH_TOKEN=&#34;&lt;token&gt;&#34;&#xA;```&#xA;&#xA;Now, let&#39;s start with the Golang program code.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;    &#34;fmt&#34;&#xA;    &#34;os&#34;&#xA;)&#xA;&#xA;func main() {&#xA;    dbURL := os.Getenv(&#34;TURSO_DATABASE_URL&#34;)&#xA;    dbToken := os.Getenv(&#34;TURSO_AUTH_TOKEN&#34;)&#xA;    dbUrl := fmt.Sprintf(&#34;%s?authToken=%s&#34;, dbURL, dbToken)&#xA;}&#xA;```&#xA;&#xA;This will be the basic config to grab the database URL and the authentication token, then we can construct the `dbURL` along with `dbToken` to construct the complete dbURL which will allow to access the database.&#xA;&#xA;Moving ahead, we will import `database/sql` package that will be used to open the database connection and `github.com/tursodatabase/libsql-client-go/libsql` to connect to the remote LibSQL database.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;database/sql&#34;&#xA;    &#34;fmt&#34;&#xA;    &#34;os&#34;&#xA;&#xA;&#x9;_ &#34;github.com/tursodatabase/libsql-client-go/libsql&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;dbURL := os.Getenv(&#34;TURSO_DATABASE_URL&#34;)&#xA;&#x9;dbToken := os.Getenv(&#34;TURSO_AUTH_TOKEN&#34;)&#xA;&#x9;dbUrl := fmt.Sprintf(&#34;%s?authToken=%s&#34;, dbURL, dbToken)&#xA;&#xA;&#x9;db, err := sql.Open(&#34;libsql&#34;, dbUrl)&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to open db %s: %s&#34;, dbUrl, err)&#xA;&#x9;&#x9;os.Exit(1)&#xA;&#x9;}&#xA;&#x9;defer db.Close()&#xA;&#xA;}&#xA;```&#xA;&#xA;The `sql.Open` function will open the connection to the database, this will return a `sql.DB` object. The driver selected is `libsql` and the `dbURL` is the entire URL along with the authentication token.&#xA;&#xA;&#xA;```go&#xA;type Post struct {&#xA;&#x9;Id          int&#xA;&#x9;Title       string&#xA;&#x9;Description string&#xA;&#x9;Content     string&#xA;}&#xA;&#xA;rows, err := db.Query(&#34;SELECT * FROM posts&#34;)&#xA;if err != nil {&#xA;    fmt.Fprintf(os.Stderr, &#34;failed to query: %s&#34;, err)&#xA;    os.Exit(1)&#xA;}&#xA;&#xA;for rows.Next() {&#xA;    var post Post&#xA;    if err := rows.Scan(&amp;post.Id, &amp;post.Title, &amp;post.Description, &amp;post.Content); err != nil {&#xA;        fmt.Fprintf(os.Stderr, &#34;failed to scan: %s&#34;, err)&#xA;        os.Exit(1)&#xA;    }&#xA;    fmt.Println(post)&#xA;}&#xA;defer rows.Close()&#xA;```&#xA;&#xA;Now, let&#39;s query some data from the database. We can construct the `Post` struct that will be used to grab the required fields like `Id`, `Title`, `Description`, and `Content` from the `posts` table in the database.&#xA;&#xA;Then, we will use the `db.Query` function to query the database. This function takes in a query and returns a `sql.Rows` object. We will iterate over all the `rows` returned from the database, with the `rows.Next()` that will fetch each row. Then we can `row.Scan` the row object with the appropriate and respective fields returned in the row. In this case, the `Id`, `Title`, `Description`, and the `Content` is fetched and stored into the `post` fields.&#xA;&#xA;We have fetched the rows and we can do operations on them as required, this was just a basic example. So the entire code can be found below.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;database/sql&#34;&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;os&#34;&#xA;&#xA;&#x9;_ &#34;github.com/tursodatabase/libsql-client-go/libsql&#34;&#xA;)&#xA;&#xA;type Post struct {&#xA;&#x9;Id          int&#xA;&#x9;Title       string&#xA;&#x9;Description string&#xA;&#x9;Content     string&#xA;}&#xA;&#xA;func main() {&#xA;&#x9;dbURL := os.Getenv(&#34;TURSO_DATABASE_URL&#34;)&#xA;&#x9;dbToken := os.Getenv(&#34;TURSO_AUTH_TOKEN&#34;)&#xA;&#x9;dbUrl := fmt.Sprintf(&#34;%s?authToken=%s&#34;, dbURL, dbToken)&#xA;&#xA;&#x9;db, err := sql.Open(&#34;libsql&#34;, dbUrl)&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to open db %s: %s&#34;, dbUrl, err)&#xA;&#x9;&#x9;os.Exit(1)&#xA;&#x9;}&#xA;&#x9;defer db.Close()&#xA;&#xA;&#x9;rows, err := db.Query(&#34;SELECT * FROM posts&#34;)&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to query: %s&#34;, err)&#xA;&#x9;&#x9;os.Exit(1)&#xA;&#x9;}&#xA;&#xA;&#x9;for rows.Next() {&#xA;&#x9;&#x9;var post Post&#xA;&#x9;&#x9;if err := rows.Scan(&amp;post.Id, &amp;post.Title, &amp;post.Description, &amp;post.Content); err != nil {&#xA;&#x9;&#x9;&#x9;fmt.Fprintf(os.Stderr, &#34;failed to scan: %s&#34;, err)&#xA;&#x9;&#x9;&#x9;os.Exit(1)&#xA;&#x9;&#x9;}&#xA;&#x9;&#x9;fmt.Println(post)&#xA;&#x9;}&#xA;&#x9;defer rows.Close()&#xA;&#xA;}&#xA;```&#xA;The output of the above code will result in all the rows present in the post table of the LibSQL database.&#xA;&#xA;```bash&#xA;$ go run remote.go&#xA;&#xA;{1 test title test description test content}&#xA;{2 test title test description test content}&#xA;{3 sample post libsql tutorial create db, connect, create tables, insert rows, sync}&#xA;{4 test title test description test content}&#xA;```&#xA;&#xA;I have added a few more rows to the post table, as you can see we have successfully connected, inserted, and read from the post table in the LibSQL database hosted on Turso.&#xA;&#xA;For all the code related to this article, you can check out the [Let&#39;s Go with Turso](https://github.com/mr-destructive/lets-go-with-turso) GitHub repo for all the examples and additional examples for using LibSQL with Golang.&#xA;&#xA;## Conclusion&#xA;&#xA;So, that is a wrap for this part of the series, we have explored how to connect a remote LibSQL database hosted on Turso with Golang. In the next part of the series, we will explore how to create embedded replicas on Turso&#39;s LibSQL database in Golang.&#xA;&#xA;Thank you for reading this post, If you have any questions, feedback, and suggestions, feel free to drop them in the comments.&#xA;&#xA;Happy Coding :)&#xA;</content>
      <type>posts</type>
    </item>
    <item>
      <title>Golang: Sort Package Introduction</title>
      <link>https://www.meetgor.com/til/golang-sort-package-basic</link>
      <description>Understanding the fundamentals of the sort package in Golang. Sorting integers, slices, struct values, maps</description>
      <pubDate>Mon, 15 Jan 2024 00:00:00 UTC</pubDate>
      <content>I have been learning Golang for around 2 years now, and I have never paid attention to the sort package, can you believe that! Where was this package hiding?&#xA;&#xA;The `sort` package provides convenient methods for sorting slices and user-defined collections in Go. Here&#39;s a quick overview of what it can do:&#xA;&#xA;## Sorting Slices&#xA;&#xA;To sort a slice of builtin types like ints, strings, etc, you can simply call `sort.Slice()`:&#xA;&#xA;```go&#xA;nums := []int{5, 2, 6, 3, 1}&#xA;&#xA;sort.Slice(nums, func(i, j int) bool {&#xA;    return nums[i] &lt; nums[j]&#xA;})&#xA;&#xA;// or &#xA;// sorts.Ints(nums)&#xA;&#xA;fmt.Println(nums)&#xA;```&#xA;&#xA;```bash&#xA;$ go run main.go&#xA;&#xA;[1, 2, 3, 5, 6]&#xA;```&#xA;The sort is in-place, mutating the original slice. You can customize the sort order by providing a less(i, j int) bool function.&#xA;&#xA;## Sorting Struct Slices&#xA;&#xA;To sort a slice of custom structs, add a Len(), Less(i, j int) bool, and Swap(i, j int) method:&#xA;&#xA;```go&#xA;type Person struct {&#xA;  Name string&#xA;  Age  int&#xA;}&#xA;&#xA;people := []Person{&#xA;  {&#34;Bob&#34;, 30},&#xA;  {&#34;John&#34;, 20},&#xA;  {&#34;Alice&#34;, 25},&#xA;}&#xA;&#xA;sort.Slice(people, func(i, j int) bool {&#xA;  return people[i].Age &lt; people[j].Age&#xA;}) &#xA;&#xA;fmt.Println(person)&#xA;```&#xA;&#xA;```bash&#xA;$ go run main.go&#xA;[{&#34;Name&#34;:&#34;Alice&#34;,&#34;Age&#34;:25},{&#34;Name&#34;:&#34;Bob&#34;,&#34;Age&#34;:30},{&#34;Name&#34;:&#34;John&#34;,&#34;Age&#34;:20}]&#xA;```&#xA;&#xA;This will sort people by age.&#xA;&#xA;## Sorting Maps&#xA;&#xA;Maps are inherently unordered in Go. We can&#39;t sort them, but we can iterate them in a sorted way. We need a separate slice of keys or values(whicher required), we will sort those keys/values and iterate over them in that order.:&#xA;&#xA;### Sort by Keys&#xA;&#xA;To sort a map by keys, we can use the `sort.Strings()` function or any other sort function as per the data structure, there are functions like [sort.Ints](https://pkg.go.dev/sort#Ints), [sort.Float64](https://pkg.go.dev/sort#Float64s), or [sort.Slice](https://pkg.go.dev/sort#Slice). We can create a new slice of keys from the map and then apply the sort on that newly created slice. After the slice of keys is created, we can iterate over it and access the map values in a order of sorted keys.&#xA;&#xA;```go&#xA;counts := map[string]int{&#xA;  &#34;hello&#34;: 5,&#xA;  &#34;world&#34;: 2,&#xA;  &#34;foo&#34;: 3,&#xA;}&#xA;&#xA;keys := make([]string, 0, len(counts))&#xA;// extract keys &#xA;for k := range counts {&#xA;  keys = append(keys, k)&#xA;} &#xA;&#xA;// sort keys&#xA;sort.Strings(keys) &#xA;&#xA;// iterate with the sorted keys slice&#xA;for _, k := range keys {&#xA;  fmt.Println(k, counts[k]) &#xA;}&#xA;```&#xA;&#xA;```bash&#xA;$ go run main.go&#xA;&#xA;foo 3&#xA;hello 5&#xA;world 2&#xA;```&#xA;&#xA;This prints the map ordered by key. You can sort by value too.&#xA;&#xA;### Sort by Value&#xA;&#xA;To iterate the map with sorting order of values, we can approach it in a similar way. We create a slice of keys. This time, we don&#39;t sort the keys, instead, we change the position of the slice of keys depending on the values. This changes the key order based on the sorted values in the map. By similarly iterating over the key slice, we iterate over the map in a sorted order of the values.&#xA;&#xA;```go&#xA;counts := map[string]int{&#xA;  &#34;hello&#34;: 5,&#xA;  &#34;world&#34;: 2,&#xA;  &#34;foo&#34;: 3,&#xA;}&#xA;&#xA;keys := make([]string, 0, len(counts))&#xA;// extract keys&#xA;for k := range counts {&#xA;  keys = append(keys, k)&#xA;}&#xA;&#xA;// sort by value&#xA;// i.e. change the order of key based on values sort order&#xA;sort.SliceStable(keys, func(i, j int) bool {&#xA;    return counts[keys[i]] &lt; counts[keys[j]]&#xA;})&#xA;&#xA;// iterate sorted&#xA;for _, k := range keys {&#xA;  fmt.Println(k, counts[k])&#xA;}&#xA;```&#xA;&#xA;```bash&#xA;$ go run main.go&#xA;&#xA;world 2&#xA;foo 3&#xA;hello 5&#xA;```&#xA;&#xA;The `sort.SliceStable` function is used to sort the slice in place. It takes in a slice and the less function which is the actual logic for comparison in the values. This function sorts the key elements based on the values in the map, and thereby the key slice is shuffled by the sorted order of the values in the map.&#xA;&#xA;The sort package is super useful for quickly organizing Go data structures.&#xA;There are [Find](https://pkg.go.dev/sort#Find) which tries to return a index of the first element that satisfies a comparison condition with a flag or found or not with a boolean. There is also a [Search](https://pkg.go.dev/sort#Search) which is used specifically for searching elements in a sorted array, it also gives a first index of the occuring element. But that is a topic for another day!&#xA;&#xA;Thank you, Happy Coding :)</content>
      <type>til</type>
    </item>
    <item>
      <title>Golang: Test Output JSON</title>
      <link>https://www.meetgor.com/til/golang-test-output-json</link>
      <description>Obtain JSON output of test results in Golang</description>
      <pubDate>Mon, 01 Jan 2024 00:00:00 UTC</pubDate>
      <content>I just discovered that we can generate a JSON output of test results in Golang. I found this [here](https://youtu.be/cf72gMBrsI0?t=80).&#xA;&#xA;Let&#39;s take a fresh simple example.&#xA;&#xA;```go&#xA;package jsontest&#xA;&#xA;func hello() string {&#xA;    return &#34;Hello, World!&#34;&#xA;}&#xA;```&#xA;```go&#xA;package jsontest&#xA;&#xA;import (&#xA;    &#34;testing&#34;&#xA;)&#xA;&#xA;func TestHello(t *testing.T) {&#xA;    want := &#34;Hello, World!&#34;&#xA;    got := hello()&#xA;    t.Logf(&#34;got: %q&#34;, got)&#xA;    if got != want {&#xA;        t.Errorf(&#34;got: %q, want: %q&#34;, got, want)&#xA;    }&#xA;}&#xA;```&#xA;&#xA;Here, we have a function `hello` that simply returns a string, the `TestHello` function in the `jsontest` package will check if the returned string is correctly returned or not.&#xA;&#xA;So, we can test this with `go test ./...` command, this will give out the output in a standard output/error in text format. However, if we add the `-json` flag, we can get the output in JSON format.&#xA;&#xA;&#xA;```bash&#xA;go test ./... -json&#xA;```&#xA;&#xA;```json&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.861974085+05:30&#34;,&#34;Action&#34;:&#34;start&#34;,&#34;Package&#34;:&#34;json-test&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863133332+05:30&#34;,&#34;Action&#34;:&#34;run&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Test&#34;:&#34;TestHello&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863142397+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Output&#34;:&#34;=== RUN   TestHello\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863148346+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Output&#34;:&#34;    main_test.go:10: got: \&#34;Hello, World\&#34;\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863151351+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Output&#34;:&#34;    main_test.go:12: got: \&#34;Hello, World\&#34;, want: \&#34;Hello, World!\&#34;\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863157014+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Output&#34;:&#34;--- FAIL: TestHello (0.00s)\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863160418+05:30&#34;,&#34;Action&#34;:&#34;fail&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Elapsed&#34;:0}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863411555+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Output&#34;:&#34;FAIL\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863438344+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Output&#34;:&#34;FAIL\tjson-test\t0.001s\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T20:52:45.863443461+05:30&#34;,&#34;Action&#34;:&#34;fail&#34;,&#34;Package&#34;:&#34;json-test&#34;,&#34;Elapsed&#34;:0.001}&#xA;```&#xA;&#xA;Pretty cool right?&#xA;&#xA;This is really useful for programmatically taking the output and parsing it to get the metrics.&#xA;&#xA;&#xA;We can even combine with the coverage flag to get the coverage metrics as well.&#xA;&#xA;```bash&#xA;go test ./... -json -cover&#xA;```&#xA;&#xA;```json&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.771976961+05:30&#34;,&#34;Action&#34;:&#34;start&#34;,&#34;Package&#34;:&#34;jsontest&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.775118482+05:30&#34;,&#34;Action&#34;:&#34;run&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestHello&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.775172535+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Output&#34;:&#34;=== RUN   TestHello\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.775201647+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Output&#34;:&#34;    main_test.go:10: got: \&#34;Hello, World!\&#34;\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.775231759+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Output&#34;:&#34;--- PASS: TestHello (0.00s)\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.775253928+05:30&#34;,&#34;Action&#34;:&#34;pass&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestHello&#34;,&#34;Elapsed&#34;:0}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.775269402+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Output&#34;:&#34;PASS\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.776153185+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Output&#34;:&#34;coverage: 100.0% of statements\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.776808599+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Output&#34;:&#34;ok  \tjsontest\t0.004s\tcoverage: 100.0% of statements\n&#34;&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:13:30.777814589+05:30&#34;,&#34;Action&#34;:&#34;pass&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Elapsed&#34;:0.006}&#xA;```&#xA;&#xA;I am planning to use this in my workflow for integrating the output of the tests suite with specific tests.&#xA;&#xA;For running the specific tests, you can use `go test -run TestName` command, this will only run the provided test function.&#xA;&#xA;```go&#xA;// main.go&#xA;&#xA;package jsontest&#xA;&#xA;func hello() string {&#xA;    return &#34;Hello, World!&#34;&#xA;}&#xA;&#xA;func add(x, y int) int {&#xA;    return x + y&#xA;}&#xA;```&#xA;&#xA;```go&#xA;// main_test.go&#xA;package jsontest&#xA;&#xA;import (&#xA;&#x9;&#34;testing&#34;&#xA;)&#xA;&#xA;func TestHello(t *testing.T) {&#xA;    want := &#34;Hello, World!&#34;&#xA;    got := hello()&#xA;    t.Logf(&#34;got: %q&#34;, got)&#xA;    if got != want {&#xA;        t.Errorf(&#34;got: %q, want: %q&#34;, got, want)&#xA;    }&#xA;}&#xA;&#xA;func TestAdd(t *testing.T) {&#xA;    want := 2&#xA;    got := add(1, 1)&#xA;    t.Logf(&#34;got: %d&#34;, got)&#xA;    if got != want {&#xA;        t.Errorf(&#34;got: %d, want: %d&#34;, got, want)&#xA;    }&#xA;}&#xA;```&#xA;&#xA;So, we have two test in this go module, we can run a specific test using `go test -run TestName` command as so:&#xA;&#xA;```bash&#xA;go test -run TestAdd -json&#xA;```&#xA;&#xA;```json&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.19397581+05:30&#34;,&#34;Action&#34;:&#34;start&#34;,&#34;Package&#34;:&#34;jsontest&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.198067398+05:30&#34;,&#34;Action&#34;:&#34;run&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestAdd&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.198150156+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestAdd&#34;,&#34;Output&#34;:&#34;=== RUN   TestAdd\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.198197444+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestAdd&#34;,&#34;Output&#34;:&#34;    main_test.go:19: got: 2\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.198217057+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestAdd&#34;,&#34;Output&#34;:&#34;--- PASS: TestAdd (0.00s)\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.198230965+05:30&#34;,&#34;Action&#34;:&#34;pass&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Test&#34;:&#34;TestAdd&#34;,&#34;Elapsed&#34;:0}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.198241628+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Output&#34;:&#34;PASS\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.19869148+05:30&#34;,&#34;Action&#34;:&#34;output&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Output&#34;:&#34;ok  \tjsontest\t0.004s\n&#34;}&#xA;{&#34;Time&#34;:&#34;2024-01-01T21:33:44.198822637+05:30&#34;,&#34;Action&#34;:&#34;pass&#34;,&#34;Package&#34;:&#34;jsontest&#34;,&#34;Elapsed&#34;:0.005}&#xA;```&#xA;&#xA;As we can see, there is only one test being executed and the output of the test is in JSON format.&#xA;&#xA;These are really good flags and options to have as they make the output more portable. I will be planning to use this to improve my workflow in testing and developing open source projects and personal projects as well. I am really inspired by the Teej&#39;s video of executing anything in NeoVim.</content>
      <type>til</type>
    </item>
    <item>
      <title>Read a Rss Feed with a URL in Golang</title>
      <link>https://www.meetgor.com/til/golang-read-rss-feed</link>
      <description>Reading Rss Feed with a Rss XML Link/URL in golang using encoding package</description>
      <pubDate>Fri, 11 Nov 2022 00:00:00 UTC</pubDate>
      <content>## Reding Rss Feed&#xA;&#xA;We can use golang&#39;s [encoding/xml](https://pkg.go.dev/encoding/xml) package to read a Rss feed. Though we have to be speicific of what type of structure the Rss feed has, so it is not dynamic but it works really well with structs. I have covered a few nuances of reading XML file in the [config file reading](https://www.meetgor.com/golang-config-file-read/#reading-xml-file) post of the 100 days of golang series.&#xA;&#xA;### Get request to Rss feed&#xA;&#xA;We first need to send a `GET` request to the Rss feed, we can use the [http](https://pkg.go.dev/net/http) package to grab the response.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;log&#34;&#xA;&#x9;&#34;net/http&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#xA;&#x9;url := &#34;https://meetgor.com/rss.xml&#34;&#xA;&#x9;response, err := http.Get(url)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;    log.Println(response.Body)&#xA;&#x9;defer response.Body.Close()&#xA;&#xA;}&#xA;```&#xA;&#xA;So, in the above example, we have used the `net/http` package to send a `GET` request with the [Get](https://pkg.go.dev/net/http#Get) funciton. The function takes in a string as a `URL` and returns either the object as response or an error. If there arose any error, we simply exit out of the program and log the error. If the error is `nil`, we return the response in the `response` variable. This builds up a good foundation for the next step to read the response body and fetching the actual bytes from the response.&#xA;&#xA;### Fetch the content from the Link&#xA;&#xA;Since we have the `response` object, we can use the [io.ReadAll](https://pkg.go.dev/io#ReadAll) function to read the bytes in the response body. The function takes in the [Reader](https://pkg.go.dev/io#Reader) object in this case it is [ReadCloser](https://pkg.go.dev/io#ReadCloser) object as a http object. The function then returns the slice of bytes/int8. The slice then can be interpreted as string or other form data that can be used for parsing the xml from the response.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;log&#34;&#xA;&#x9;&#34;net/http&#34;&#xA;    &#34;io&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#xA;&#x9;url := &#34;https://meetgor.com/rss.xml&#34;&#xA;&#x9;response, err := http.Get(url)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;&#x9;data, err := io.ReadAll(response.Body)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;    log.Println(string(data))&#xA;    log.Printf(&#34;Type -&gt; %T&#34;, data)&#xA;}&#xA;```&#xA;&#xA;```&#xA;&lt;rss&gt;&#xA;    &lt;channel&gt;&#xA;        &lt;item&gt;&#xA;        ...&#xA;        ...&#xA;        ...&#xA;        &lt;/item&gt;&#xA;    &lt;/channel&gt;&#xA;&lt;/rss&gt;&#xA;&#xA;&#xA;Type -&gt; []uint8 &#xA;&#xA;```&#xA;&#xA;So, we can see that the parsed content is indeed xml, it is type casted to string from the slice of bytes. This can be further used for the parsing the text as Rss structure and fetch the required details.&#xA;&#xA;## Parsing Rss with a struct&#xA;&#xA;We can now move into creating a struct for individual tags required in the parsing.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;    &#34;encoding/xml&#34;&#xA;&#x9;&#34;io&#34;&#xA;&#x9;&#34;log&#34;&#xA;&#x9;&#34;net/http&#34;&#xA;)&#xA;&#xA;type Rss struct {&#xA;&#x9;XMLName xml.Name `xml:&#34;rss&#34;`&#xA;&#x9;Channel Channel  `xml:&#34;channel&#34;`&#xA;}&#xA;&#xA;type Channel struct {&#xA;&#x9;XMLName     xml.Name `xml:&#34;channel&#34;`&#xA;&#x9;Title       string   `xml:&#34;title&#34;`&#xA;&#x9;Description string   `xml:&#34;description&#34;`&#xA;&#x9;Item        []Item   `xml:&#34;item&#34;`&#xA;}&#xA;&#xA;type Item struct {&#xA;&#x9;XMLName xml.Name `xml:&#34;item&#34;`&#xA;&#x9;Title   string   `xml:&#34;title&#34;`&#xA;&#x9;Link    string   `xml:&#34;link&#34;`&#xA;}&#xA;&#xA;func main() {&#xA;&#xA;&#x9;url := &#34;https://meetgor.com/rss.xml&#34;&#xA;&#x9;response, err := http.Get(url)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;&#x9;data, err := io.ReadAll(response.Body)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;    log.Println(string(data))&#xA;}&#xA;```&#xA;&#xA;If you would look at the [rss feed](https://meetgor.com/rss.xml), you can see it has a structure of tags and elements. The `rss` tag is the root tag, followed by `channel` and other types of nested tags speicific for the type of information to be stored like `title` for the title in the feed, `link` for the link to the feed, etc. &#xA;&#xA;So, we create those as structure, the root structure is the `Rss` which we will create with a few attributes like `Channel` and the name of the current tag. In the `Rss` case the name of the tag/element is `rss`, so it is given the `xml.Name` as `xml:&#39;rss&#39;` in backticks indicating the type hint for the field. The next field is the `Channel` which is another type(custom type struct). We have defined `Channel` as a struct just after it that will hold information like the `title`, `description` of the website. We also have the `xml.Name` as `xml:&#34;channel&#34;` which indicates the current struct is representation of `channel` tag in the rss feed. Finally, we also have a custom type struct as `Item`. The `Item` struct has a few attributes like `Title`, `Link` and you can now start to see the pattern, you can customize it as per your requirements and speicifications.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;    &#34;encoding/xml&#34;&#xA;&#x9;&#34;io&#34;&#xA;&#x9;&#34;log&#34;&#xA;&#x9;&#34;net/http&#34;&#xA;)&#xA;&#xA;type Rss struct {&#xA;&#x9;XMLName xml.Name `xml:&#34;rss&#34;`&#xA;&#x9;Channel Channel  `xml:&#34;channel&#34;`&#xA;}&#xA;&#xA;type Channel struct {&#xA;&#x9;XMLName     xml.Name `xml:&#34;channel&#34;`&#xA;&#x9;Title       string   `xml:&#34;title&#34;`&#xA;&#x9;Description string   `xml:&#34;description&#34;`&#xA;&#x9;Item        []Item   `xml:&#34;item&#34;`&#xA;}&#xA;&#xA;type Item struct {&#xA;&#x9;XMLName xml.Name `xml:&#34;item&#34;`&#xA;&#x9;Title   string   `xml:&#34;title&#34;`&#xA;&#x9;Link    string   `xml:&#34;link&#34;`&#xA;}&#xA;&#xA;func main() {&#xA;&#xA;&#x9;url := &#34;https://meetgor.com/rss.xml&#34;&#xA;&#x9;response, err := http.Get(url)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;&#x9;data, err := io.ReadAll(response.Body)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;    // New Code&#xA;&#xA;&#x9;d := Rss{}&#xA;&#x9;err = xml.Unmarshal(data, &amp;d)&#xA;&#xA;&#x9;if err != nil {&#xA;&#x9;&#x9;log.Fatal(err)&#xA;&#x9;}&#xA;&#xA;&#x9;for _, item := range d.Channel.Item {&#xA;&#x9;&#x9;log.Println(item.Title)&#xA;&#x9;}&#xA;}&#xA;```&#xA;&#xA;```&#xA;$ go run main.go&#xA;&#xA;Why and How to make and use Vim as a text editor and customizable IDE&#xA;Setting up Vim for Python&#xA;Setting up Vim for BASH Scripting&#xA;Vim: Keymapping Guide&#xA;...&#xA;...&#xA;...&#xA;Django + HTMX CRUD application&#xA;PGCLI: Postgres from the terminal&#xA;Golang: Closures&#xA;Golang: Interfaces&#xA;Golang: Error Handling&#xA;Golang: Paths&#xA;Golang: File Reading&#xA;Golang: JSON YAML TOML (config) File Reading.&#xA;```&#xA;&#xA;So, here we have initialized the `Rss` struct as empty and then used the [Unmarshal](https://pkg.go.dev/encoding/xml#Unmarshal) method in the `xml` package. The Unmarshal method will parse the data as per the type of either int, float, bool or string, any other type of data will be discarded as interface or struct. We can usually parse any valid type of data into `Unmarshal` method and it generally gives a proper expected outcome.&#xA;&#xA;The Unmarshal method takes in the slice of byte and the second paramter as pointer to a struct or any variable that will store the parsed xml content from the slice of byte. The function just returns the error type, either `nil` in case of no errors, and returns the actual error obejct if there arise any type of error.&#xA;&#xA;So we parse the `data` which is a slice of byte to the funciton and the reference to the `d` object which is a empty `Rss` object. This will get us the data in the `d` object. We can then iterate over the object as per the struct and use the perform operations like type casting or converting types, etc to get your required data back.&#xA;&#xA;In the above example, we simply iterate over the `d.Channel.Item` which is a list of elements of tag `item` in the rss feed. Inside the for loop, we can access the object and simply print or perform any sort of operations. I have simply printed the list of articles with titles. &#xA;&#xA;Links for the code available on the [100 days of golang](https://github.com/Mr-Destructive/100-days-of-golang/blob/main/scripts/files/read/config_files/xml/rss.go) GitHub repository.&#xA;&#xA;So, that&#39;s how we parse an XML feed in golang. Just plug and play if you have a similar type of structure of the Rss XML feed. Happy Coding :)</content>
      <type>til</type>
    </item>
    <item>
      <title>Golang: Interfaces</title>
      <link>https://www.meetgor.com/posts/golang-interfaces</link>
      <description>Introduction In the 19th post of the series, we will be taking a look into interfaces in golang. Interfaces allow us to create function signatures common to dif</description>
      <pubDate>Sat, 10 Sep 2022 00:00:00 UTC</pubDate>
      <content>## Introduction&#xA;&#xA;In the 19th post of the series, we will be taking a look into interfaces in golang. Interfaces allow us to create function signatures common to different structs or types. So, we can allow multiple structs to have a common interface(method) that can have different implementations.&#xA;&#xA;## What are Interfaces&#xA;&#xA;Interface as the name suggests is a way to create methods that are common to different structures or types but can have different implementations. It&#39;s an interface to define the method or function signatures but not the implementation. Let&#39;s take an example of `Laptop` and `Phone` having the functionality of wifi. We can connect to wifi more or the less in a similar way on both devices. The implementation behind the functionality might be different but they share the same operation. The WiFi can act as an interface for both devices to connect to the internet.&#xA;&#xA;## Define an Interface&#xA;&#xA;To declare an interface in golang, we can use the `interface` keyword in golang. An interface is created with the type keyword, providing the name of the interface and defining the function declaration. Inside the interface which acts as a struct of general method signatures. The method signatures usually consist of the name of the function with its parameters if any and the return type of the function.&#xA;&#xA;```go&#xA;&#xA;package main&#xA;&#xA;import &#34;fmt&#34;&#xA;&#xA;&#xA;type Player struct {&#xA;&#x9;name   string&#xA;&#x9;health int&#xA;}&#xA;&#xA;type Mob struct {&#xA;&#x9;name     string&#xA;&#x9;health   int&#xA;&#x9;is_passive bool&#xA;}&#xA;&#xA;type Creature interface {&#xA;&#x9;intro() string&#xA;&#x9;//attack() int&#xA;&#x9;//heal() int&#xA;}&#xA;&#xA;func main() {&#xA;&#x9;player := Player{name: &#34;Steve&#34;}&#xA;&#x9;mob := Mob{name: &#34;Zombie&#34;}&#xA;&#x9;fmt.Println(player)&#xA;&#x9;fmt.Println(mob)&#xA;}&#xA;```&#xA;&#xA;```&#xA;go run main.go&#xA;&#xA;{Steve 0}&#xA;{Zombie 0 false}&#xA;```&#xA;&#xA;In this above example, we have created an interface called `Creature`. There are a few structs that we have to define like `Player` and `Mob`, these two methods have a few attributes like `name` as `string` and `health` as `int` which are common in both structs but the `Mob` struct has an additional attribute `is_passive` as a `boolean` value. The `Creature` is an interface that will define certain function signatures, here we have declared `intro`, `attack`, and `heal` as the methods bound to the Creature interface. This means, that any object which satisfies the Creature interface, can define the methods associated with the interface.&#xA;&#xA;## Defining Interfaces&#xA;&#xA;Once we have declared the interface method signatures, we can move into defining the functionality of these methods depending on the struct. If we want a different working method for different types of struct objects passed we can define those for each type of struct that we want. Here, we have two types of structs namely `Creature` and `Mob`, based on the struct we can define the `intro` method for them individually.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import &#34;fmt&#34;&#xA;&#xA;type Creature interface {&#xA;&#x9;intro() string&#xA;&#x9;attack(*int) int&#xA;}&#xA;&#xA;type Player struct {&#xA;&#x9;name   string&#xA;&#x9;health int&#xA;}&#xA;&#xA;type Mob struct {&#xA;&#x9;name     string&#xA;&#x9;health   int&#xA;&#x9;category bool&#xA;}&#xA;&#xA;func (p Player) intro() string {&#xA;&#x9;fmt.Println(&#34;Player has spawned&#34;)&#xA;&#x9;return p.name&#xA;}&#xA;&#xA;func (p Player) attack(m_health *int) int {&#xA;&#x9;fmt.Println(&#34;Player has attacked!&#34;)&#xA;&#x9;*m_health = *m_health - 50&#xA;&#x9;return *m_health&#xA;}&#xA;&#xA;func (m Mob) intro() string {&#xA;&#x9;fmt.Printf(&#34;A wild %s has appeared!&#xA;&#34;, m.name)&#xA;&#x9;return m.name&#xA;}&#xA;func (m Mob) attack(p_health *int) int {&#xA;&#x9;fmt.Printf(&#34;%s has attacked you! -%d&#xA;&#34;, m.name, 30)&#xA;&#x9;*p_health = *p_health - 30&#xA;&#x9;return *p_health&#xA;}&#xA;&#xA;func main() {&#xA;&#x9;player := Player{name: &#34;Steve&#34;, health: 100}&#xA;&#x9;mob := Mob{name: &#34;Zombie&#34;, health: 140}&#xA;&#x9;fmt.Println(player.intro())&#xA;&#x9;fmt.Println(mob.intro())&#xA;&#x9;fmt.Println(mob)&#xA;&#x9;fmt.Println(player)&#xA;&#x9;fmt.Println(player.attack(&amp;mob.health))&#xA;&#x9;fmt.Println(mob.attack(&amp;player.health))&#xA;&#x9;fmt.Println(mob)&#xA;&#x9;fmt.Println(player)&#xA;}&#xA;```&#xA;&#xA;```&#xA;go run main.go&#xA;&#xA;Player has spawned&#xA;Steve&#xA;A wild Zombie has appeared!&#xA;Zombie&#xA;{Zombie 140 false}&#xA;{Steve 100}&#xA;Player has attacked!&#xA;90&#xA;Zombie has attacked you! -30&#xA;70&#xA;{Zombie 90 false}&#xA;{Steve 70}&#xA;```&#xA;&#xA;As we can see, the method `intro()` is bound to both the struct depending on what struct signature is associated with the method. The method `intro` takes in the object struct associated as per the call and returns `string` as defined in the method signature. &#xA;&#xA;The `attack` method in the `Creature` interface is also implemented separately for the two structs. For the `Player` method, we simply take in a pointer to an integer and return an `int`. The parameter is the pointer to the mob health, and it returns the modified health. We take in a pointer to the mob or player health so as to parse in the actual value and not the copy of the value. If we modify the value, we want to reflect those changes in the actual object. So that is how we can use interfaces to construct dynamic operations on objects as well as different types of structs.&#xA;&#xA;We have seen a simple example of how to declare and define interfaces for given type structs. Also, we can pass by value as well as by pointers so as to define the behavior of the method whether to dynamically modify or change the values of the object associated with it.&#xA;&#xA;## Examples of Interfaces&#xA;&#xA;There are quite some use cases of interfaces, in object-oriented programming, the above example fits the polymorphism feature quite well. The ability to reuse certain method signatures and define the functions as per requirement brings flexibility to the code structure.&#xA;&#xA;We will see a few examples for understanding how we can use interfaces in various ways.&#xA;&#xA;### Type Switch Interface&#xA;&#xA;We can use an empty interface to check for the type of variable we have parsed. Using this empty interface we can create a kind of dynamic parameter to a function.  &#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;strconv&#34;&#xA;)&#xA;&#xA;func parse_int(n interface{}) int {&#xA;&#x9;switch n.(type) {&#xA;&#x9;case int:&#xA;&#x9;&#x9;return (n).(int) * (n).(int)&#xA;&#x9;case string:&#xA;&#x9;&#x9;s, _ := strconv.Atoi(n.(string))&#xA;&#x9;&#x9;return s&#xA;&#x9;case float64:&#xA;&#x9;&#x9;return int(n.(float64))&#xA;&#x9;default:&#xA;&#x9;&#x9;return n.(int)&#xA;&#x9;}&#xA;}&#xA;&#xA;func main() {&#xA;&#x9;num := parse_int(4)&#xA;&#x9;fmt.Println(num)&#xA;&#x9;num = parse_int(&#34;4&#34;)&#xA;&#x9;fmt.Println(num)&#xA;&#x9;num = parse_int(4.1243)&#xA;&#x9;fmt.Println(num)&#xA;}&#xA;&#xA;```&#xA;&#xA;```&#xA;go run main.go&#xA;&#xA;16&#xA;4&#xA;4&#xA;```&#xA;&#xA;Here, we can see we have an interface as a parameter to the function `parse_int`, the return type is `int`, so the incoming parameter can be any valid type. But if we don&#39;t convert the given type into an appropriate int, it will result in an error as we are returning the int value of the parsed parameter. We are taking the parameter as `interface{}` which is an empty interface, this will contain the parameter parsed as an interface type. That&#39;s why we need to convert the interface object into an int or the parsed type of the interface.&#xA;&#xA;### Interface Slice&#xA;&#xA;We can even create a slice of interfaces, which means we can initialize or group together various objects of different structs in a single slice. This might be helpful for calling functions associated with different objects via the interface very easily and in a much cleaner way.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import &#34;fmt&#34;&#xA;&#xA;type Creature interface {&#xA;&#x9;intro() string&#xA;}&#xA;&#xA;type Player struct {&#xA;&#x9;name   string&#xA;&#x9;health int&#xA;}&#xA;&#xA;type Mob struct {&#xA;&#x9;name     string&#xA;&#x9;health   int&#xA;&#x9;category bool&#xA;}&#xA;&#xA;func (p Player) intro() string {&#xA;&#x9;fmt.Println(&#34;Player has spawned&#34;)&#xA;&#x9;return p.name&#xA;}&#xA;&#xA;func (m Mob) intro() string {&#xA;&#x9;var name string&#xA;&#x9;if m.name != &#34;&#34; {&#xA;&#x9;&#x9;name = m.name&#xA;&#x9;} else {&#xA;&#x9;&#x9;name = &#34;Mob&#34;&#xA;&#x9;}&#xA;&#x9;fmt.Printf(&#34;A wild %s has appeared!&#xA;&#34;, name)&#xA;&#x9;return m.name&#xA;}&#xA;&#xA;&#xA;func main() {&#xA;&#x9;entity := []Creature{Player{}, Mob{}, Mob{}, Player{}}&#xA;&#xA;&#x9;for _, obj := range entity {&#xA;&#x9;&#x9;fmt.Println(obj.intro())&#xA;&#x9;}&#xA;}&#xA;```&#xA;&#xA;```&#xA;Player has spawned&#xA;A wild Zombie has appeared!&#xA;A wild Zombie has appeared!&#xA;Player has spawned&#xA;```&#xA;&#xA;In the above example, we can see that the entity variable is created as a slice of interfaces `Creature`, i.e. various objects associated with the `Creature` interface can be contained in a single slice. There are 2 instances of Player and Mob each in the slice. We can further iterate over the slice as a range-based loop and thereby the functions associated with the interfaces can be called. Here, we have called the `intro` function.&#xA;&#xA;So, there are a lot of things that can be done with interfaces, we can create multiple interfaces for a single object struct and nest interfaces. Based on the use case of the program, interfaces can be used to reduce the boilerplate code as well as improve the readability of the code.&#xA;&#xA;That&#39;s it from this part. Reference for all the code examples and commands can be found in the [100 days of Golang](https://github.com/mr-destructive/100-days-of-golang/tree/main/scripts/interfaces/main.go) GitHub repository.&#xA;&#xA;## Conclusion&#xA;&#xA;From this part of the series, we were able to understand the basics of interfaces using a few examples. We explored how interfaces can be used to bring in polymorphism in golang, also we can improve the readability of the code. The boilerplate code can be considerably reduced by using interfaces when dealing with structs and types. Hopefully, you found this post helpful and understood even the basics of interfaces in golang. Thank you for reading, if you have any queries, questions, or feedback, you can ping me on my social handles or in the comments. Happy Coding :)&#xA;</content>
      <type>posts</type>
    </item>
    <item>
      <title>Golang: Math Package</title>
      <link>https://www.meetgor.com/posts/golang-math</link>
      <description>Introduction Moving on in the 100 days of golang series, we can take a look into the math package in golang&#39;s standard library. In programming, math is quite cr</description>
      <pubDate>Mon, 25 Apr 2022 00:00:00 UTC</pubDate>
      <content>## Introduction&#xA;&#xA;Moving on in the 100 days of golang series, we can take a look into the math package in golang&#39;s standard library. In programming, math is quite critical aspect, we need to perform certain mathematical operations quite regularly so golang&#39;s standard library has a package for serving some quite commonly used math functions and procedures. We&#39;ll take a look at some of the basic and common functions which are available in the math package.&#xA;&#xA;## Mathematical Constants&#xA;&#xA;We have some constants like `pi`, `e`, `Phi` already defined as constants in the math package of the standard library in golang. They have a precision till 15 digits stored in float64 values. &#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;math&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;fmt.Println(&#34;Pi = &#34;, math.Pi)&#xA;&#x9;fmt.Println(&#34;E = &#34;, math.E)&#xA;&#x9;fmt.Println(&#34;Phi = &#34;, math.Phi)&#xA;&#x9;fmt.Println(&#34;Sqrt of 2 = &#34;, math.Sqrt2)&#xA;&#x9;fmt.Println(&#34;Naturla Log 2 = &#34;, math.Ln2)&#xA;&#x9;fmt.Println(&#34;Naturla Log 10 = &#34;, math.Ln10)&#xA;}&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/constants.go&#xA;Pi =  3.141592653589793&#xA;E =  2.718281828459045&#xA;Phi =  1.618033988749895&#xA;Sqrt of 2 =  1.4142135623730951&#xA;Naturla Log 2 =  0.6931471805599453&#xA;Naturla Log 10 =  2.302585092994046&#xA;```&#xA;&#xA;We can use these constants in trigonometric calculations and also in scientific computing. Further, you can get a list of all constants defined in the math package of the go standard library from the [documentation](https://pkg.go.dev/math#pkg-constants).&#xA;&#xA;## Basic Math functions&#xA;&#xA;We have some quite basic and fundamental functions in the math package that can be used commonly in many programs. Let&#39;s take a look at a few of them.&#xA;&#xA;### - Abs :parameters (float64) , returns float64&#xA;&#xA;As the name suggest, the [Abs](https://pkg.go.dev/math#Abs) it returns the absolute result of a numbers. It takes a parameter as a float64 value and returns the absolute value of the provided number as a `float64` number.&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;math&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;a := 45&#xA;&#x9;b := 100&#xA;&#x9;diff := a - b&#xA;&#x9;fmt.Println(diff)&#xA;&#xA;&#x9;absolute_diff := math.Abs(float64(a) - float64(b))&#xA;&#x9;fmt.Println(absolute_diff)&#xA;}&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/main.go&#xA;-55&#xA;55&#xA;```&#xA;&#xA;As, we can see the `Abs` function takes in a float64 value and returns a absolute value of the given number that too a `float64` value. We need to cast the numbers `a` and `b` into `float64` as we have not provided the initial values and so the compiler has assigned the type to them as `int`. &#xA;&#xA;#### Type Casting&#xA;&#xA;We can caste a type into other by using the variable around the type name as `type_name(variable)`. In the above example we have converted the `int` value `45` into a `float64` as `float64(45)` which again yields `45` but as a float64 type. &#xA;&#xA;```go&#xA;foo := 77&#xA;fmt.Printf(&#34;Type of foo = %T &#xA;&#34;, foo)&#xA;fmt.Println(&#34;foo = &#34;, int(foo))&#xA;fmt.Println(&#34;String Cast: &#34;, string(foo))&#xA;fmt.Println(&#34;Float Cast: &#34;, float64(foo))&#xA;```&#xA;&#xA;Though not every type cannot be casted into due to quite oblivious reasons, for instance `77` or any other integer value (except for 0 or 1) cannot be converted into boolean value.&#xA;&#xA;**Hello Gopher! Just a small note, the math package almost deals with float64 types rather than int to avoid backwards compatibility to perform operations on floating point values which can be casted into integers rather than defining separate functions for decimal values and integers.**&#xA;&#xA;### - Min/Max: parameters(float64) , returns float64&#xA;&#xA;We can get the [minimum](https://pkg.go.dev/math#Min) and [maximum](https://pkg.go.dev/math#Max) value of the two numbers provided to the function.&#xA;&#xA;```go&#xA;var float64 a = 120&#xA;var float64 b = 54&#xA;&#xA;minimum := math.Min(float64(a), float64(b))&#xA;maximum := math.Max(float64(a), float64(b))&#xA;fmt.Printf(&#34;Min of %v and %v is %v &#xA;&#34;, a, b, minimum)&#xA;fmt.Printf(&#34;Max of %v and %v is %v &#xA;&#34;, a, b, maximum)&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/main.go&#xA;Min of 120 and 54 is 54&#xA;Max of 120 and 54 is 120&#xA;```&#xA;&#xA;### - Pow  : parameters(float64, float64) , returns float64&#xA;### - Pow10: parameters(int)              , returns float64&#xA;&#xA;The [Pow](https://pkg.go.dev/math#Pow) function is used to get the exponential result of the base number. So, if we provide the values x and y, we would get the result as the number x raised to y. &#xA;&#xA;```go&#xA;var x float64 = 3&#xA;var y float64 = 4&#xA;z := math.Pow(x, y)&#xA;z10 := math.Pow10(int(x))&#xA;fmt.Println(&#34;X ^ Y = &#34;, z)&#xA;fmt.Println(&#34;10 ^ X = &#34;, z10)&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/main.go&#xA;X ^ Y =  81&#xA;10 ^ X =  1000&#xA;```&#xA;&#xA;We also have the [Pow10](https://pkg.go.dev/math#Pow10) function which works just like the pow function except the x value is 10 and we don&#39;t have to provide it, there is just one parameter as a integer which returns a float64 value.&#xA;&#xA;### - Sqrt: parameters(float64) , returns float64&#xA;&#xA;The [Sqrt](https://pkg.go.dev/math#Sqrt) function as the name suggest, it is used to get the square root value of a floating point value which returns a float64 value.&#xA;&#xA;```go&#xA;var k float64 = 125&#xA;sqrt_of_k := math.Sqrt(k)&#xA;cbrt_of_k := math.Cbrt(k)&#xA;&#xA;fmt.Printf(&#34;Square root of %v = %v &#xA;&#34;, k, sqrt_of_k)&#xA;fmt.Printf(&#34;Cube root of %v = %v &#xA;&#34;, k, cbrt_of_k)&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/main.go&#xA;Square root of 125 = 11.180339887498949&#xA;Cube root of 125 = 5&#xA;```&#xA;&#xA;### - Trunc: parameters(float64) , returns float64&#xA;&#xA;The [Truncate](https://pkg.go.dev/math#Trunc) function provides the way to round off a decimal value(float64) to an integer but it returns a value in `float64`.&#xA;&#xA;```go&#xA;var p float64 = 445.235&#xA;trunc_p := math.Trunc(p)&#xA;fmt.Printf(&#34;Truncated value of %v = %v &#xA;&#34;, p, trunc_p)&#xA;p = 123.678&#xA;trunc_p = math.Trunc(p)&#xA;fmt.Printf(&#34;Truncated value of %v = %v &#xA;&#34;, p, trunc_p)&#xA;&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/main.go&#xA;Truncated value of 445.235 = 445&#xA;Truncated value of 123.678 = 123&#xA;```&#xA;&#xA;### - Ceil : parameters(float64) , returns float64&#xA;&#xA;We also can use the [Ceil](https://pkg.go.dev/math#Ceil) function to roud up the value to the next integer value but the value is returned as `float64`. &#xA;&#xA;```go&#xA;var c float64 = 33.25&#xA;ceil_c := math.Ceil(c)&#xA;fmt.Printf(&#34;Ceiled value of %v = %v &#xA;&#34;, c, ceil_c)&#xA;c = 134.78&#xA;ceil_c = math.Ceil(c)&#xA;fmt.Printf(&#34;Ceiled value of %v = %v &#xA;&#34;, c, ceil_c)&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/main.go&#xA;Ceiled value of 33.25 = 34&#xA;Ceiled value of 134.78 = 135&#xA;```&#xA;&#xA;### - Trigonometric Functions&#xA;&#xA;Trigonometric functions are quite helpful that can help in intense mathematical computations in backend projects or precision dependent projects. We have functions [Sin](https://pkg.go.dev/math#Sin), [Cos](https://pkg.go.dev/math#Cos), [SinCos](https://pkg.go.dev/math#Sincos), [Tan](https://pkg.go.dev/math#Tan), hyperbolic functions in Trigonometric functions like [Sinh](https://pkg.go.dev/math#Sinh), [Cosh](https://pkg.go.dev/math#Cosh), [Tanh](https://pkg.go.dev/math#Tanh), and Inverse Trigonometric functions like [Asin](https://pkg.go.dev/math#Asin), [Asinh](https://pkg.go.dev/math#Asinh), etc. &#xA;&#xA;- Sin: parameters(float64) , returns float64&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;math&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;// basic trigonometric functions&#xA;&#x9;var x float64 = math.Pi / 2&#xA;&#x9;sinx := math.Sin(x)&#xA;&#x9;cosx := math.Cos(x)&#xA;&#x9;tanx := math.Tan(x)&#xA;&#x9;fmt.Printf(&#34;Sin(%v) = %v &#xA;&#34;, x, sinx)&#xA;&#x9;fmt.Printf(&#34;Cos(%v) = %v &#xA;&#34;, x, cosx)&#xA;&#x9;fmt.Printf(&#34;Tan(%v) = %v &#xA;&#34;, x, tanx)&#xA;&#xA;&#x9;// hyperbolic trigonometric functions&#xA;&#x9;var h float64 = math.Pi / 2&#xA;&#x9;sinh := math.Sinh(h)&#xA;&#x9;cosh := math.Cosh(h)&#xA;&#x9;tanh := math.Tanh(h)&#xA;&#x9;fmt.Printf(&#34;Sinh(%v) = %v &#xA;&#34;, h, sinh)&#xA;&#x9;fmt.Printf(&#34;Cosh(%v) = %v &#xA;&#34;, h, cosh)&#xA;&#x9;fmt.Printf(&#34;Tanh(%v) = %v &#xA;&#34;, h, tanh)&#xA;&#xA;&#x9;// Inverse Trigonometric functions&#xA;&#x9;var y float64 = -1&#xA;&#x9;arc_sin := math.Asin(y) // -pi/2 radians or 90 degrees&#xA;&#x9;arc_cos := math.Acos(y) // pi randians or 180 degrees&#xA;&#x9;arc_tan := math.Atan(y) &#xA;&#x9;fmt.Printf(&#34;Sin^-1(%v) = %v &#xA;&#34;, y, arc_sin)&#xA;&#x9;fmt.Printf(&#34;Cos^-1(%v) = %v &#xA;&#34;, y, arc_cos)&#xA;&#x9;fmt.Printf(&#34;Tan^-1(%v) = %v &#xA;&#34;, y, arc_tan)&#xA;&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/trignometric.go&#xA;Sin(1.5707963267948966) = 1&#xA;Cos(1.5707963267948966) = 6.123233995736757e-17&#xA;Tan(1.5707963267948966) = 1.6331239353195392e+16&#xA;Sinh(1.5707963267948966) = 2.3012989023072947&#xA;Cosh(1.5707963267948966) = 2.5091784786580567&#xA;Tanh(1.5707963267948966) = 0.9171523356672744&#xA;Sin^-1(-1) = -1.5707963267948966&#xA;Cos^-1(-1) = 3.141592653589793&#xA;Tan^-1(-1) = -0.7853981633974483&#xA;```&#xA;&#xA;Here we can see that the functions are working fine and giving a decently precise value. This might be enough for simple and smaller projects, though for higher precision and accuracy areas, other computations and programming is required to compute the values.&#xA;&#xA;### - Exponential and Logarithmic Functions&#xA;&#xA;We also have the exponential and logarithmic functions defined in the math package to leverage computations realted to formulae that deal with logarithmic or exponential calculations. &#xA;&#xA;- Exp  : parameters(flaot64) , returns float64&#xA;- Exp2 : parameters(flaot64) , returns float64&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;math&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;// exponential function&#xA;&#x9;var x float64 = 2&#xA;&#x9;y := math.Exp(x)&#xA;&#x9;fmt.Println(&#34;e^x = &#34;, y)&#xA;&#x9;var n float64 = 3.5&#xA;&#x9;y = math.Exp2(n)&#xA;&#x9;fmt.Println(&#34;2^n = &#34;, y)&#xA;&#xA;&#x9;// Logarithmic function&#xA;&#x9;y = math.Log(x)&#xA;&#x9;fmt.Println(&#34;natural log x = &#34;, y)&#xA;&#xA;&#x9;n = 128&#xA;&#x9;y = math.Log2(n)&#xA;&#x9;fmt.Println(&#34;Log2 of 100 = &#34;, y)&#xA;}&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/expo_log.go&#xA;e^x =  7.38905609893065&#xA;2^n =  11.31370849898476&#xA;natural log x =  0.6931471805599453&#xA;Log2 of 100 =  7&#xA;```&#xA;&#xA;Here, we have exponential functions such as `e^x` and `2^n` which might be useful in some common programming calculations. Also the logarithmic functions like `log x` which is natural log of x(base e), and `log2 n` which is logn to the base 2.&#xA;&#xA;## The Random package&#xA;&#xA;The `random` sub-package in golang provides some great tools for working with random numbers and generating them. It provides exhaustive list of functions and types that help in generating pseudo random numbers. &#xA;&#xA;- Int : parameters() , returns int&#xA;- Intn : parameters( int ) , returns int&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;math/rand&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;// random integer generation&#xA;&#x9;x := rand.Int()&#xA;&#x9;fmt.Println(x)&#xA;&#xA;&#x9;// random number generation till range&#xA;&#x9;for i := 0; i &lt; 5; i++ {&#xA;&#x9;&#x9;y := rand.Intn(10)&#xA;&#x9;&#x9;fmt.Println(y)&#xA;&#x9;}&#xA;}&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/rand.go&#xA;5577006791947779410&#xA;7&#xA;7&#xA;9&#xA;1&#xA;8&#xA;```&#xA;&#xA;In the above example, we have used the [Int](https://pkg.go.dev/math/rand@go1.18.1#Int) function in the random sub-package of the math package which generates a pseudo random integer of the range dependent on the system architecture generally `int32` or `int64`. We get a huge number which is pseudo random i.e. not truly random. If you try to execute the program a couple of time, you would notice the number remains the same and we are calling it random? Well we need to dive into random numbers and seeding for a different part of the series for sure. &#xA;&#xA;The [Intn](https://pkg.go.dev/math/rand@go1.18.1#Intn) function also generates a pseudo random number but this time, we define the range of the upper boundary to generate them. It is not inclusive of the number provided i.e. we have provide the value `10` so the number `10` is not included in the range. It&#39;s called half open interval. It starts from 0 so the range becomes mathematically `[0, n)` if n is the number provided to the Intn function. &#xA;&#xA;## The Bits package&#xA;&#xA;We also have a bit sub-package in the math package of the go standard library. This sub package is used for working around with bit manipulation and operations at the binary level. This is quite helpful in competitive programming , also in understanding the basics of data structures and fundamentals in computer science.&#xA;&#xA;- Add       : parameters(uint, uint, uint) , returns uint, uint &#xA;- Len       : parameters(uint) , returns int&#xA;- OnesCount : parameters(uint) , returns int&#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;math/bits&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#x9;s, c := bits.Add(0, 9, 1)&#xA;&#x9;fmt.Printf(&#34;Sum = %d &#xA;Carry = %d &#xA;&#34;, s, c)&#xA;&#xA;&#x9;// (45) in decimal = (1 0 1 1 0 1) in binary&#xA;&#x9;var n uint = 45&#xA;&#x9;length := bits.Len(n)&#xA;&#x9;ones_in_45 := bits.OnesCount(n)&#xA;&#x9;fmt.Printf(&#34;Minimum bits required to represent 45 = %d &#xA;&#34;, length)&#xA;&#x9;fmt.Printf(&#34;Set Bits in 45 = %d &#xA;&#34;, ones_in_45)&#xA;&#xA;}&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/bit.go&#xA;Sum = 10&#xA;Carry = 0&#xA;Minimum bits required to represent 45 = 6&#xA;Set Bits in 45 = 4&#xA;```&#xA;&#xA;Here, in the above example, we have used the bits sub pacakge in the math package, the [Add](https://pkg.go.dev/math/bits@go1.18.1#Add) function allows us to provide the two numbers and a carry bit on which it returns two values the sum and the carry. The sum is defined as the summation of `x + y + carry` the two numbers and the carry bit. The carry bit needs to be either 0 or 1. &#xA;&#xA;Also the value provided the function i.e. `x and y` need to be unsigned `uint` iorder to work with bits. &#xA;&#xA;We also have the [Len](https://pkg.go.dev/math/bits@go1.18.1#Len) function which returns the maximum number of bits required to represent the provided unsigned integer. We have used 45 which is equivalent to `10110` and hence the function returns `6` as teh number of bits. The [OnesCount](https://pkg.go.dev/math/bits@go1.18.1#OnesCount) Function is also similar but it returns the number of set bits(the `1` bit) in the number provided to it. &#xA;&#xA;We&#39;ll see this sub package in a separate section of its own. Bits is really a great pacakge to work with bits and low level manipulation of numbers in Golang.&#xA;&#xA;## The Complex package&#xA;&#xA;The complex subpackage is really specific to the operation to the complex numbers and its operations. Using complex numbers with basic operations and trigonometric functions are provided in the package. &#xA;&#xA;```go&#xA;package main&#xA;&#xA;import (&#xA;&#x9;&#34;fmt&#34;&#xA;&#x9;&#34;math/cmplx&#34;&#xA;)&#xA;&#xA;func main() {&#xA;&#xA;&#x9;x := complex(5, 8)&#xA;&#x9;y := complex(3, 4)&#xA;&#x9;mod_x := cmplx.Abs(x)&#xA;&#x9;mod_y := cmplx.Abs(y)&#xA;&#x9;conj_x := cmplx.Conj(x)&#xA;&#x9;phase_x := cmplx.Phase(x)&#xA;&#x9;mod, phase := cmplx.Polar(x)&#xA;&#xA;&#x9;fmt.Println(&#34;x = &#34;, x)&#xA;&#x9;fmt.Println(&#34;Modulus of x = &#34;, mod_x)&#xA;&#x9;fmt.Println(&#34;Modulus of y = &#34;, mod_y)&#xA;&#x9;fmt.Println(&#34;Conjugate of x = &#34;, conj_x)&#xA;&#x9;fmt.Println(&#34;Phase of x = &#34;, phase_x)&#xA;&#x9;fmt.Printf(&#34;Polar Form : %v, %v&#xA;&#34;, mod, phase)&#xA;&#xA;}&#xA;```&#xA;&#xA;```&#xA;$ go run basic-functions/complex.go&#xA;x =  (5+8i)&#xA;Modulus of x =  9.433981132056603&#xA;Modulus of y =  5&#xA;Conjugate of x =  (5-8i)&#xA;Phase of x =  1.0121970114513341&#xA;Polar Form : 9.433981132056603, 1.0121970114513341&#xA;&#xA;```&#xA;&#xA;We have used the complex function to create complex numbers. The `cmplx` subpackage in the math package provides many functions to play with trignometric and simple operations with complex numbers. The [Abs](https://pkg.go.dev/math/cmplx@go1.18.1#Abs) function is used to get the modulus of the provided complex number. The modulus is calculated with `sqrt(x^2 + y^2)`, this gives the magnitude of the complex number. Here, we get the modulus as `9.43` as `sqrt(25 + 64)` for the complex number `5+8i`. Also, for `3+4i` the modulus becomes `sqrt(9+16)` which turns out to be `5`. The [Conjugate](https://pkg.go.dev/math/cmplx@go1.18.1#Conj) function is used to get the conjugate of the provided complex number. &#xA;&#xA;Also the phase or the Argument of the complex number can be obtained with the [Phase](https://pkg.go.dev/math/cmplx@go1.18.1#Phase) function. The phase is caluculated by the formula `tan^-1 (y/x)` but the angle is returned in randians. So for `x = 5+8i` the argument/Phase becomes `tan^-1( 8/5)` which is `57.995 degrees` or `1.012 radians`. &#xA;&#xA;We have the [Polar](https://pkg.go.dev/math/cmplx@go1.18.1#Polar) function which gives the polar form of the complex number i.e. `(modulus r, phase theta)` So this function returns two values the modulus and the argument/phase of the complex number. We have already calcualted both the values but this functions gets both of them in a single function. Quite neat, we can even ignore one value after the return of the function by using the ignore operator `_, phase := cmplx.Polar(5+7i)` to only care and get the phase/argument of the complex number or `modulus, _ := cmplx.Polar(5+7i)` to get the modulus from the complex number.&#xA;&#xA;So that&#39;s some basic operations on complex numbers, this might have very few use cases but it&#39;s still quite useful when needed. &#xA;&#xA;That&#39;s it from this part. Reference for all the code examples and commands can be found in the [100 days of Golang](https://github.com/mr-destructive/100-days-of-golang/) GitHub repository.&#xA;&#xA;## Conclusion&#xA;&#xA;So from this section we were able to get a bit deeper introduction to the `math` package in golang&#39;s standard library. We covered some few important functions and constants in the main math package along with the glimpse of other subpackages like `rand`, `cmplx` and `bits`. We didn&#39;t get too much in detail with those sub packages as they can be explored on a separate section of their own. Hopefully, you have got a godd overview of the math package in golang which again is really important aspect in programming.&#xA;&#xA;Thank you for reading. If you have any questions or feedback, please let me know in the comments or on social handles. Happy Coding :)&#xA;</content>
      <type>posts</type>
    </item>
  </channel>
</rss>