Day 2: The Joy of Go - Basic Concepts and Syntax
Just enough code to make you dangerous
Today is all about learning the basic of programming. Sure, we will be using Go but the lessons apply across all programming languages
We will focus on a few subjects:
The Go Playground: A fun and interactive way to explore Go without any installation or setup. Learn how to use this online environment to practice coding and experiment with your ideas.
Go Basics: A gentle introduction to Go's essential building blocks, such as variables, data types, and simple operations. You'll learn how to read and write Go code at a basic level.
Control Structures: Discover the power of decision-making and repetition in Go with if-else statements, for loops, and switch-case statements. We'll use relatable examples to make these concepts easy to grasp.
By the end of Day 2, you'll have a solid foundation in Go's basic concepts and syntax, making you ready to dive into more advanced topics and start building the CLI in the days to come.
The Go Playground: A Fun and Interactive Way to Explore Go
The Go Playground is an online platform that provides a fun and interactive way to explore and experiment with Go code. It allows you to write, run, and share Go programs directly from your web browser, without the need to install any software or set up a development environment on your computer.
Sure, this sounds like replit, the cloud environment you setup on Day 1. The playground is more for quick ideation and experimentation. I personally use it often.
The community has also built upon the idea of the Go Playground, with a similar but slightly more advanced tool (https://goplay.tools) which I highly recommend. It has syntax highlights and autocompletion, making the experience just 👌
For the examples today, we will be using https://goplay.tools.
Go Basics - A Gentle Introduction
Variables and Data Types
In Go, variables are used to store and manipulate data in a program. To declare a variable, we use the var keyword, followed by the variable name and its data type. Here's an example:
In this example, we declare a variable named age of type int (integer). Go has several basic data types, including:
intandfloat64for whole numbers and decimal numbers, respectivelystringfor textboolfor true or false values
You can also assign a value to a variable when declaring it:
The most common way to assign a value to a variable is use the short variable declaration syntax to declare and assign a value at the same time. Go can infer the type from the value:
Operators
Go has several types of operators that allow you to perform arithmetic, comparisons, and logical operations:
Arithmetic operators:
+,-,*,/,%Relational operators:
==,!=,<,>,<=,>=Logical operators:
&&(and),||(or),!(not)
For example:
Creating and Formatting Strings
To create a string in Go, you can use double quotes:
To concatenate strings, you can use the + operator.
To format strings, you can use the fmt.Sprintf() function with placeholders:
The %s placeholder is just telling the formatter that I will be using a string value.
fmt.Sprintf() essentially means, I have a string but parts of it are stored in variables. I can then format the string I have and add placeholders for interpolation in the case of variables. For example in the above case, a real world scenario would be that name and quote are not just static values but inputs from the user. So you wouldn’t know in advance how to construct the string without the input. That’s where the fmt.Sprintf() comes in handy.
Feel free to play around with it in the playground to get more familiar with it.
Control Structures - Mastering Decision-Making and Repetition
Control structures allow your program to make decisions and repeat actions based on certain conditions. It’s how humans also think about their day to day tasks: “If it rains, I will take an umbrella”. Or, “Count to 30 and the food will be ready then”.
1. If-Else Statements
If-else statements are used to make decisions in your code based on a condition. They have the following syntax:
For example:
You can also use else if to check multiple conditions:
2. For Loops
For loops are used to repeat a block of code for a specific number of iterations or until a condition is met. Go has a simple for loop syntax:
For example:
In the initialization, i is 1, and within each loop we increment its value via i++ until we reach 11 and then stop.
There are a few more structures than just the two above but for now these will suffice.
Wow we have covered a lot of ground today 🤯🔥
Let’s put what we have learnt finally into coding!
Let’s head to https://goplay.tools. You should be able to see the following:
There are a few details that we will discuss later on so don’t worry about. Just know that func main() is the entry point to your application or code.
Inside the func main() we can write our code. Let’s write the if/else example we saw earlier:
If you hit Run you should see the output in the pane below. Share would allow you to share your snippet with the world! The code above can be found https://goplay.tools/snippet/kFkZioKxKlk.
OK we are close to the end 🎉 Why don’t you try writing some code in the playground and share it with me?
A few ideas:
Fahrenheit to Celsius Converter: Write a program that converts a given temperature in Fahrenheit to Celsius. The formula to convert Fahrenheit to Celsius is (F - 32) * 5/9, where F is the temperature in Fahrenheit.
Sample Input: Fahrenheit temperature = 68
Sample Output: Temperature in Celsius = 20.0
Multiplication Table: Write a program that takes an integer input and prints the multiplication table of that number up to 5. For example, if the input is 3, then the program should print the following output:
Sample Input: Number = 3
Sample Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
I hope you find these problems helpful! See you tomorrow on Day 3 of the course.



















