Go int, float, complex: Mastering Basic Data Types in Golang šŸš€

beginner
7 min

Go int, float, complex: Mastering Basic Data Types in Golang šŸš€

Welcome to another exciting lesson on CodeYourCraft! Today, we're going to dive into the world of Go programming language and explore the fundamental data types: int, float, and complex. Let's get started! šŸŽÆ

Why Go? šŸ¤”

Go, also known as Golang, is a modern, open-source programming language designed by Google. It's known for its simplicity, efficiency, and robustness, making it an excellent choice for building a wide range of applications, from web servers to data pipelines.

Understanding Data Types šŸ“

In programming, data types are categories that define the type of value a variable can hold. Go offers several basic data types, and in this lesson, we'll focus on integers, floating-point numbers, and complex numbers.

int šŸ’”

An int (integer) is a whole number without a fractional part. Go supports two types of integers: int and int8, int16, int32, int64. The int type is platform-dependent, meaning it can be either int32 or int64.

go
// Example 1: Declaring and initializing an integer variable package main import "fmt" func main() { var myInt int = 42 fmt.Println("My integer value is:", myInt) }

float šŸ’”

A float is a real number that can have a fractional part. Go supports two types of floating-point numbers: float32 and float64. The float64 type is more precise and is the recommended choice for most cases.

go
// Example 2: Declaring and initializing a float variable package main import "fmt" func main() { var myFloat float64 = 3.14159 fmt.Println("My float value is:", myFloat) }

complex šŸ’”

A complex number is a number of the form a + bi, where a and b are real numbers, and i is the imaginary unit, i² = -1. In Go, complex numbers are represented as complex64 or complex128.

go
// Example 3: Declaring and initializing a complex number package main import "fmt" func main() { var myComplex complex64 = 1 + 2i fmt.Println("My complex number is:", myComplex) }

Quiz šŸ“

Quick Quiz
Question 1 of 1

Which Go integer type is platform-dependent?

That's it for today's lesson! In the next lesson, we'll delve deeper into Go data types and explore more advanced topics. Until then, happy coding! šŸ’»āœØ