FUN WITH GO

Watipaso Rattle Chirambo
TechMalawi
3 min readOct 15, 2022

--

INTRODUCTION

Great engineers explore, build and solve problems in the most effective way possible. Are you an engineer looking for a Swiss Army Knife to add to your toolbox? Here you GO! From creating Web Applications to writing scripts, handling micro-controllers, and the ability to compile on nearly any machine. GO is a true definition of a Swiss Army Knife Coming from a scripting background myself, I’ve used python for most of my scripting, I decided to try out Golang for an operation I had written in python and it’s safe to say I’m impressed and a Gopher now. This article will look at using Go slices where we create, read, update and delete entries we are calling thoughts. Let’s call the program “ThoughtBox”.

IMPORTANT!

  • We will take the learn-by-doing approach, for Go fundamentals visit the official Go docs.
  • We are not working with a database, third-party libraries, or custom packages.
  • You can clone the repo on GitHub.

PREREQUISITES

  • Basic programming knowledge.
  • GO installed
  • Basic command line knowledge
  • IDE or TextEditor of your choice. “I’m using vscode”

LET’S GO!

FIRST STEPS

  • Create a folder for our project, and name it what you like.
  • Create a file named main.go in our project folder.

To start, open main.go file. Inside the file let’s have the following structure.

At the top of our code snippet, we have package main which tells the GO compiler that the package should compile as an executable.

Below the package main line, we have our imports, for our project we have a few imports, it’s a small program.

Below our imports, we have our Struct which is similar to a class if you are familiar with other programming languages like Python, C#, and Java.

The entry point of our program is the main function, which we have below our Struct.

NOTE!

Functions are declared using the func keyword

mt

Inside the main function, we are creating a slice of type Thought and adding three thoughts to our struct. We are then printing our slice of thoughts with the Golang built-in fmt module.

In the code snippet above, we defined our function that takes one argument which is a slice of type Thought.

There are a couple of ways to obtain user input, in our case, we are using Golangs built-in bufio module. Since a single thought can have space in between words, we are keeping all that input including spaces no matter the thought and to do that we are using Golangs built-in bufio module.

We first prompt the user to Add a thought, when the user enters a thought we append(add) it to the slice of thoughts using the append slice method.

After user input is successful we print out that the thought was added successfully.

READ ONE

In order to get a thought from our box, we need to loop through all our elements and print all thoughts we have. The code snippet below does exactly what we’ve just discussed.

Here is the code snippet

UPDATE THOUGHT

The update function takes in three arguments, the slice of type Thought., user Input text, which modifies the element that has been chosen. Below is our code snippet.

Below we have our Delete function which takes in two arguments, the first argument is a slice of type Thought and a thoughtId. The arguments help find the exact element in order to operate on it in our case deleting a specific element.

Now we are done with our functions as well as looking at our entry point function (func main(){}).

Below is the source code for our ThoughtBox

--

--

Watipaso Rattle Chirambo
TechMalawi

I’m a Systems Developer, Technical Writer with a bachelor's Degree in Business Information Technology. I love sharing and acquiring knowledge so, LET’S SHARE