Getting Started With Go
Getting Started With Go: A brief history and understanding data types
Introduction
I didn't know about Go until I saw Danny Thompson's LinkedIn account. So I decided to just do some research about the technology. "The more you learn. The more you will learn. The moment we stop learning we stop growing".
In the world of programming languages, Go, or Golang as it is often called, has carved out a niche for itself by combining simplicity, efficiency, and strong concurrency support. Whether you are a seasoned developer or just starting out, understanding the history and core concepts of Go can give you a solid foundation as you dive into the powerful language.
A brief history of Go
Go was created at Google in 2007 by Robert Griesemer, Robe Pike, and Ken Thompson. The language was born out of a desire to address the shortcomings of existing languages in handling modern-day software engineering challenges, particularly in large-scale distributed systems.
The team behind Go aimed to create a language that was simple yet powerful, enabling developers to write efficient code without the complexity found in other languages like C++. In 2009 it was launched as an open-source programming language. in March 2012 its first stable release was officially announced, Go 1.0. Since then, it has gained widespread adoption, especially in cloud computing and microservices space.
Key Goals of Go's Design:
-
Simplicity: Go's syntax is straightforward, making it easy for developers to learn and use. We have testimonials on the official website stating that employees at Capital One were able to write in Go within a month without having prior knowledge of the language. Really motivating right?
-
Efficiency: Go is designed to be fast, with a string focus on performance and minimal memory overhead - the extra memory consumed by a system to manage and execute tasks, beyond the memory required for the task itself. Thanks to the inbuilt garbage collection.
-
Concurrency: Go's goroutines and channels provide powerful tools for managing concurrent processes, making it ideal for building scalable systems.
-
Strong Typing: Go enforces type safety, reducing common programming errors.
Go is fast, efficient and a safe language for the development and deployment of software. Since Go was developed while the ICT industry was undergoing a rapid development in infrastructure, the language was built to provide support for modern hardware architecture.
Understanding Data Types in Go
Go is a statically typed language, meaning that variables must be defined with a specific type at compile time. This ensures that the type of variable is known and consistent throughout its lifetime, leading to a safer and more predictable code. Other examples of statically typed languages include: C++, Java and Rust.
A Data Type is an attribute associated with a piece of data that tells a computer how to interpret its value. It is a classification of data which tells the compiler or interpreter how the programmer intends to use the code.
Understanding how to design our code really helps us understand the types of data we want to manipulate and how to manipulate the data.
Built-in Data Types
As other languages, Go also has its built-in types: boolean, integers, floats and strings. Let's have a dive into these types and see how you can use them idiomatically.
-
Boolean (`bool`)
The
bool
type represents a value that can either betrue
orfalse
. It's commonly used in conditional statements to control the flow of execution based on certain conditions.Example:
var hasPassed bool = true var isLoggedIn bool = false
-
Numeric Types
Some languages such as Javascript have a single numeric data type - 'number', which usually accommodates integers, decimal numbers (also called floating-point numbers) as well as numbers written in scientific notation.
That's not the case in Go. The language provides a variety of numeric types, including integers, floating-point numbers, and complex numbers.
-
Integers
-
Signed Integers - They can hold both positive and negative values. They include:
int
,int8
,int32
,int16
, andint64
. They represent integers with different sizes, corresponding to the number of bits they use. -
Unsigned Integers - They only hold non-negative numbers. They include
uint
,uint8
,uint16
,uint32
, anduint64
. They represent unsigned integers of various sizesExample:
var age int = 25 // A typical signed integer var maxUint8 uint8 = 255 // An undignrf 8-bit integer
-
Floating Point Numbers
Floating-point numbers are used to represent real numbers (numbers with a fractional component). Go provides
float32
andfloat64
, withfloat64
being the default due to its precision.Example:
var pi float64 = 3.14159 var temperature float32 = 36.6
Floating point numbers are useful in mathematical computations that require decimal points, such as calculating averages and percentages.
-
Complex Numbers
They consist of a real and an imaginary part. The types
complex64
andcomplex128
correspond to complex numbers withfloat32
andfloat64
components respectively.Example:
var c1 complex64 = 1 + 2i var c2 complex128 = complex(5, 12)
-
-
Rune (
rune
)Represents a single Unicode character, typically a
int32
.Example:
var firstLetter rune = 'K'
-
String
Represents a sequence of characters. Strings are immutable in Go. You can reassign the value of a string variable but you cannot change the value of the string that is assigned to it.
Example:
var movieName string = "Spiderman"
Conclusion
Golang's simplicity and efficiency, combined with its robust type system, make it an excellent choice for developers seeking to build scalable and maintainable software. By understanding Go's history and mastering basic Built-in data types, you are well on your way to becoming proficient in this powerful language. Stay tuned for more in-depth articles as we explore Go's features and capabilities further.
© Kandy Hamisi Said.RSS