First try at GoLang – htmlcoder utility

First try at GoLang – htmlcoder utility

See also GitHub: https://github.com/atkaper/htmlcoder-go

Intro

We want to try using GoLang for some micro services at the office, so a bit of Go study was needed for me. This article shows some of the references I used to start using Go, and it has my first bit of Go code. In the last weeks, I had to decode some html entities, and used an online html decoder web form for this. So I chose to make this html decode into a Go command line utility. It is always nice to try out a language by making something useful.

Used resources which got me started

I am running Linux Mint, which is based on Ubuntu.

GoLang installation

I installed GoLang using the installation instructions from: https://golang.org/doc/install And I added these lines to my ~/.profile:

export PATH="$PATH:/usr/local/go/bin:$HOME/go/bin"
export GOPATH="$HOME/go"
export GOROOT="/usr/local/go"

Development environment / “editor”

My first pitfall – do NOT install Visual Studio Code as “flatpak”, as that will complicate getting GoLang support working. Just install it as a “normal” package. I used this instruction (just a hit from an internet search): https://techviewleo.com/install-visual-studio-code-on-linux-mint/

# Used on Linux Mint 20 (Find proper manual for your OS!)

# Update packages, and make sure you have apt-transport-https installed
sudo apt update
sudo apt install apt-transport-https

# Add microsoft as package source
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /tmp/microsoft.gpg

sudo install -o root -g root -m 644 /tmp/microsoft.gpg /etc/apt/trusted.gpg.d/

sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'

# Update package list, and install "code" (= Visual Studio Code)
sudo apt update
sudo apt install code

After installing Visual Studio, it did ask at some point in time if I wanted GoLang support (I think when opening a *.go file). I just clicked yes, and “download all” to get that up and running.

I tend to use the command line quite often. And to start VSC for my project, I go into the working folder, and type: code . to start the editor (code is the name of VSC, and the DOT is indicating to open the current directory in it).

Note: I also tried using IntelliJ, as that is the development environment I use daily, but I could not get the GoLang plugin to work 100%. Probably some setting I can not find yet. VSC does work without issues for now.

GoLang Internet resources

From the above links, I completely went through the first two links to get a basic understanding of the language. And after that, I went on to write my first little go tool, as show in this project.

Debugging

In short, install the debugger from https://github.com/go-delve/delve/tree/master/Documentation/installation:

git clone https://github.com/go-delve/delve
cd delve
go install github.com/go-delve/delve/cmd/dlv

Afterwards, depending on the Go project you use, you need to configure a file: .vscode/launch.json
I changed the “program” attribute in that json to point to my htmlcoder.go file.

The Tool

This is a simple command line tool, which you can use to html encode or decode some data. You can either use this as a shell (pipe) filter, or you can process a file, or you can pass in the data to be encoded/decoded as command line arguments.

Build

Pre-requisites:

  • Have GoLang installed
  • Have a copy of the code: git clone https://github.com/atkaper/htmlcoder-go.git ; cd htmlcoder-go
# compile
go build htmlcoder.go

# optional: put the tool in your path for ease of use
sudo install -o root -g root -m 755 htmlcoder /usr/local/bin/

Usage

Example use:

# show help:
htmlcoder -h

# decode command line data:
htmlcoder -d -c -- ""hello"" test

# decode a file:
htmlcoder -d -f /tmp/somefile.html

# encode output from another command:
echo '"hello" test' | htmlcoder

Conclusion

It is always fun to learn a new language (or at least, I think it is). The two introductions are a nice start into getting to know the basics, and the official documentation page has much more to read about real life examples / services.

I am well aware that I’m not using the module stuff yet, and not using fancy tools for dependency management, but that is maybe something to look for in the next project.


Thijs Kaper, November 7, 2021.

Leave a Reply

Your email address will not be published. Required fields are marked *