Update Your GitHub Profile Readme With Your Latest Medium Article Automatically Using Go

A step-by-step guide to automating the little things

Koen Verburg
Better Programming

--

Laptop featuring code. To the right, a person has their feet kicked up on the table.
Photo by Joshua Aragon on Unsplash.

Your GitHub profile Readme has come a long way, but it’s missing something: your latest Medium article, of course. In this tutorial, I’m going to walk you through how to set this up using a Go script.

Requirements

You will need Go version 1.x installed on your machine. If you don’t have any Go knowledge, I would recommend checking out this article from Bryan Dijkhuizen to get a better understanding of the basics.

Getting Started

We are going to create a script that fetches our latest Medium articles and writes them out to a markdown file. First up, we need to get a data source. Medium provides an RSS feed, which is perfect for our use case. We can get this feed using the URL below. Be sure to change the username to yours:

https://medium.com/feed/@koenverburg

Create a folder or go to your GitHub profile Readme repository via the terminal.

Now let’s initialize a Go module by running the following command:

go mod init medium

Create a new file called main.go and paste in the following boilerplate code:

package mainfunc main() {}

Now let’s start by getting the request going. Paste the following code inside the main function. Don’t forget to change the username:

On one of the first lines, we are making an HTTP get request to the RSS feed from Medium. The get function returns a tuple, response, and err. It’s good to check for the error first and then proceed with the response.

After the error check, we defer on the response.Body.Close(). By doing this, the client will be closed when we are finished with it. Then we give the response.Body to ioutil.ReadAll. This is because the body is a byte stream and we want to read all of it. This will also return a tuple on which we will first check for any errors. Then we will proceed with the body and print that to the string using the string function.

Running this will get the following output:

XML output
Screenshot of the XML output.

Looking at the output of the URL feed, we are presented with XML. We have to turn that into structs so we can use the strongly typed features of the Go programming language.

Create a new file called rss.go inside a folder called structs and paste in the following code:

Back to the main function. Let’s remove this line because we are not going to need it:

fmt.Println(string(body))

And replace it with the following:

Here, we are using the XML package and we are going to unmarshal the body against the rss variable, which is of the Rss struct type. Marshaling may be a new term for you. Look at it as a conversion operation from one data format to another. Learn more about the topic on Wikipedia.

If all went well, this will output the following:

Title of the RSS feed
Screenshot of the title of the RSS feed.

Alright, now that we have everything strongly typed, let’s get the latest articles using the following loop:

for i := 0; i < 3; i++ {
fmt.Println(rss.Channel.Item[i].Title)
}

This will output the following:

Screenshot of output
Screenshot of the output: looping over the latest articles

At this point, we have our latest articles, but it’s not in markdown format. Let’s change this and write it out to markdown. Replace the aforementioned for loop for the loop below:

Here, we are creating a variable called items of the type string array. Then we enter the loop where we are creating a formatted string using the following format: [<text>](<link>). This is a markdown representation of an anchor tag in HTML. And we are adding in the title and link per iteration of the loop.

The output will look something like this:

Screenshot of output
Screenshot of the output: markdown format

Let’s write out the contents to a markdown file so we can see the whole script in action. Paste this part of the code below the loop we just created:

Here, we are creating a new file. And did you notice a pattern of tuples and error checking? If so, good! Now to the fun part: merging our template with the file. We do this using io.WriteString. This method will also return a tuple, but here, we do not care about the output, so we assign it the variable _. This means that it’s not used.

If you run this, you will have a new Readme file with your latest articles.

Screenshot of VScode
Screenshot of the VScode: markdown preview left, raw markdown right

That is it. We are done with the scripting part. Now for the automation part.

Create a .github/workflows folder and a cron.yml. This is where the magic happens. This script will run at a certain interval. It will check out your code, run the main.go file, and commit the changes to the repo!

name: cron on:
push:
branches:
- master
schedule:
- cron: "5 5 * * *"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master
with:
fetch-depth: 1
- name: Generate
run: |
cd ${GITHUB_WORKSPACE}/
go run main.go
- name: Commit
run: |
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git add .
git commit -am "feat(auto generate): Update content"
git push --all -f <https://$>{{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git

Now you know how to get your latest articles using Go and write them out to a markdown file in a completely automated way. The last step is for you to push this up to GitHub and see the magic happen. Good luck!

Conclusion

Thank you for reading. In this article, I talked about:

--

--

Software Engineer based in Rotterdam — Photographer, Art Enthusiast, Dreamer, and thinker.